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

Commit 4cd3cd2

Browse files
committed
Improve fix for sending empty token if worker was delayed
- 6e3a7b7 was a good idea but messes up any existing pending workers, since the 'old' token is no longer available. Instead, when there is a new token cancel all outstanding workers and schedule a new one that updates all users. This makes sure that 1) old/new is handled as expected and 2) any changes to the currently viewed user are immediately visible (due to LiveData).
1 parent 668bce2 commit 4cd3cd2

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import androidx.appcompat.widget.SwitchCompat;
2323
import androidx.coordinatorlayout.widget.CoordinatorLayout;
2424
import androidx.fragment.app.Fragment;
25+
import androidx.work.WorkManager;
2526

2627
import com.google.android.material.snackbar.Snackbar;
2728
import com.google.firebase.iid.FirebaseInstanceId;
@@ -36,6 +37,7 @@
3637
import nl.jpelgrm.movienotifier.data.APIHelper;
3738
import nl.jpelgrm.movienotifier.data.AppDatabase;
3839
import nl.jpelgrm.movienotifier.models.User;
40+
import nl.jpelgrm.movienotifier.service.FcmRefreshWorker;
3941
import nl.jpelgrm.movienotifier.ui.view.DoubleRowIconPreferenceView;
4042
import nl.jpelgrm.movienotifier.ui.view.IconSwitchView;
4143
import nl.jpelgrm.movienotifier.util.ErrorUtil;
@@ -460,7 +462,8 @@ private void verifyFCMToken() {
460462
String storedToken = notificationSettings.getString("token", "");
461463
String receivedToken = task.getResult().getToken();
462464
if(!storedToken.equals(receivedToken)) {
463-
notificationSettings.edit().putString("token", receivedToken).apply();
465+
WorkManager.getInstance().cancelAllWorkByTag("fcmRefresh");
466+
WorkManager.getInstance().enqueue(FcmRefreshWorker.getRequestToUpdateImmediately(receivedToken, null));
464467
}
465468
}
466469
});

0 commit comments

Comments
 (0)