Skip to content
This repository was archived by the owner on Jun 5, 2022. It is now read-only.

Commit 6e3a7b7

Browse files
committed
Fix sending empty token if worker was delayed
- If the worker that stores the new token has not yet been executed and the user manually toggles push notifications in settings, an empty/outdated token may be sent. To prevent this from happening, when opening the user settings page, request the latest FCM token and update the stored value if necessary.
1 parent e25aaae commit 6e3a7b7

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

app/src/main/java/nl/jpelgrm/movienotifier/service/FcmRefreshWorker.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@
33
import android.content.Context;
44
import android.content.SharedPreferences;
55

6-
import java.io.IOException;
7-
import java.util.Arrays;
8-
import java.util.Collections;
9-
import java.util.List;
10-
116
import androidx.annotation.NonNull;
127
import androidx.annotation.Nullable;
138
import androidx.work.Constraints;
@@ -16,6 +11,11 @@
1611
import androidx.work.OneTimeWorkRequest;
1712
import androidx.work.Worker;
1813
import androidx.work.WorkerParameters;
14+
15+
import java.io.IOException;
16+
import java.util.Collections;
17+
import java.util.List;
18+
1919
import nl.jpelgrm.movienotifier.data.APIHelper;
2020
import nl.jpelgrm.movienotifier.data.AppDatabase;
2121
import nl.jpelgrm.movienotifier.models.User;

app/src/main/java/nl/jpelgrm/movienotifier/ui/settings/SettingsAccountOverviewFragment.java

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import android.os.Build;
88
import android.os.Bundle;
99
import android.provider.Settings;
10+
import android.util.Log;
1011
import android.view.LayoutInflater;
1112
import android.view.View;
1213
import android.view.ViewGroup;
@@ -15,17 +16,19 @@
1516
import android.widget.ScrollView;
1617
import android.widget.TextView;
1718

18-
import com.google.android.material.snackbar.Snackbar;
19-
20-
import java.util.ArrayList;
21-
import java.util.Collections;
22-
2319
import androidx.annotation.NonNull;
2420
import androidx.annotation.Nullable;
2521
import androidx.appcompat.app.AlertDialog;
2622
import androidx.appcompat.widget.SwitchCompat;
2723
import androidx.coordinatorlayout.widget.CoordinatorLayout;
2824
import androidx.fragment.app.Fragment;
25+
26+
import com.google.android.material.snackbar.Snackbar;
27+
import com.google.firebase.iid.FirebaseInstanceId;
28+
29+
import java.util.ArrayList;
30+
import java.util.Collections;
31+
2932
import butterknife.BindView;
3033
import butterknife.ButterKnife;
3134
import nl.jpelgrm.movienotifier.BuildConfig;
@@ -90,6 +93,8 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
9093

9194
settings = getContext().getSharedPreferences("settings", Context.MODE_PRIVATE);
9295
notificationSettings = getContext().getSharedPreferences("notifications", Context.MODE_PRIVATE);
96+
97+
verifyFCMToken();
9398
}
9499

95100
@Nullable
@@ -180,7 +185,11 @@ private void togglePushNotifications() {
180185
boolean setToEnabled = notificationsPush.isChecked();
181186
if(setToEnabled) {
182187
if(!toUpdate.getFcmTokens().contains(token)) {
183-
changed = toUpdate.getFcmTokens().add(token);
188+
if(!token.equals("")) {
189+
changed = toUpdate.getFcmTokens().add(token);
190+
} else {
191+
notificationsPush.setChecked(false);
192+
}
184193
}
185194
} else {
186195
if(toUpdate.getFcmTokens().contains(token)) {
@@ -445,6 +454,18 @@ public void onFailure(@NonNull Call<ResponseBody> call, @NonNull Throwable t) {
445454
}).setNegativeButton(R.string.no, null).show();
446455
}
447456

457+
private void verifyFCMToken() {
458+
FirebaseInstanceId.getInstance().getInstanceId().addOnCompleteListener(task -> {
459+
if(task.isSuccessful() && task.getResult() != null) {
460+
String storedToken = notificationSettings.getString("token", "");
461+
String receivedToken = task.getResult().getToken();
462+
if(!storedToken.equals(receivedToken)) {
463+
notificationSettings.edit().putString("token", receivedToken).apply();
464+
}
465+
}
466+
});
467+
}
468+
448469
private void setFieldsEnabled(boolean enabled) {
449470
accountSwitch.setClickable(enabled);
450471
accountName.setClickable(enabled);

0 commit comments

Comments
 (0)