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

Commit

Permalink
Fix sending empty token if worker was delayed
Browse files Browse the repository at this point in the history
 - 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.
  • Loading branch information
jpelgrom committed Apr 18, 2019
1 parent e25aaae commit 6e3a7b7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@
import android.content.Context;
import android.content.SharedPreferences;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.work.Constraints;
Expand All @@ -16,6 +11,11 @@
import androidx.work.OneTimeWorkRequest;
import androidx.work.Worker;
import androidx.work.WorkerParameters;

import java.io.IOException;
import java.util.Collections;
import java.util.List;

import nl.jpelgrm.movienotifier.data.APIHelper;
import nl.jpelgrm.movienotifier.data.AppDatabase;
import nl.jpelgrm.movienotifier.models.User;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -15,17 +16,19 @@
import android.widget.ScrollView;
import android.widget.TextView;

import com.google.android.material.snackbar.Snackbar;

import java.util.ArrayList;
import java.util.Collections;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.SwitchCompat;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.fragment.app.Fragment;

import com.google.android.material.snackbar.Snackbar;
import com.google.firebase.iid.FirebaseInstanceId;

import java.util.ArrayList;
import java.util.Collections;

import butterknife.BindView;
import butterknife.ButterKnife;
import nl.jpelgrm.movienotifier.BuildConfig;
Expand Down Expand Up @@ -90,6 +93,8 @@ public void onCreate(@Nullable Bundle savedInstanceState) {

settings = getContext().getSharedPreferences("settings", Context.MODE_PRIVATE);
notificationSettings = getContext().getSharedPreferences("notifications", Context.MODE_PRIVATE);

verifyFCMToken();
}

@Nullable
Expand Down Expand Up @@ -180,7 +185,11 @@ private void togglePushNotifications() {
boolean setToEnabled = notificationsPush.isChecked();
if(setToEnabled) {
if(!toUpdate.getFcmTokens().contains(token)) {
changed = toUpdate.getFcmTokens().add(token);
if(!token.equals("")) {
changed = toUpdate.getFcmTokens().add(token);
} else {
notificationsPush.setChecked(false);
}
}
} else {
if(toUpdate.getFcmTokens().contains(token)) {
Expand Down Expand Up @@ -445,6 +454,18 @@ public void onFailure(@NonNull Call<ResponseBody> call, @NonNull Throwable t) {
}).setNegativeButton(R.string.no, null).show();
}

private void verifyFCMToken() {
FirebaseInstanceId.getInstance().getInstanceId().addOnCompleteListener(task -> {
if(task.isSuccessful() && task.getResult() != null) {
String storedToken = notificationSettings.getString("token", "");
String receivedToken = task.getResult().getToken();
if(!storedToken.equals(receivedToken)) {
notificationSettings.edit().putString("token", receivedToken).apply();
}
}
});
}

private void setFieldsEnabled(boolean enabled) {
accountSwitch.setClickable(enabled);
accountName.setClickable(enabled);
Expand Down

0 comments on commit 6e3a7b7

Please sign in to comment.