Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit 5d5130f

Browse files
Merge pull request #1229 from PeterStaev/fix-android-token
fix android token generation workflow
2 parents 32b92c7 + b89bdf0 commit 5d5130f

File tree

1 file changed

+23
-3
lines changed
  • src/platforms/android/libraryproject/firebase/src/main/java/org/nativescript/plugins/firebase

1 file changed

+23
-3
lines changed

src/platforms/android/libraryproject/firebase/src/main/java/org/nativescript/plugins/firebase/FirebasePlugin.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public class FirebasePlugin {
1111

1212
private static String cachedToken;
1313
private static String cachedNotification;
14+
private static boolean preventInitialRegisterTokenCallback = false;
1415

1516
private static FirebasePluginListener onPushTokenReceivedCallback;
1617
private static FirebasePluginListener onNotificationReceivedCallback;
@@ -19,7 +20,13 @@ public static void registerForPushNotifications(final String senderId) {
1920
new Thread() {
2021
public void run() {
2122
try {
22-
FirebaseInstanceId.getInstance().getToken(senderId, "FCM");
23+
String token = FirebaseInstanceId.getInstance().getToken(senderId, "FCM");
24+
25+
if (!preventInitialRegisterTokenCallback) {
26+
executeOnPushTokenReceivedCallback(token);
27+
}
28+
29+
preventInitialRegisterTokenCallback = false;
2330
} catch (IOException e) {
2431
Log.e(TAG, "Error getting a token from FCM: " + e.getMessage(), e);
2532
}
@@ -44,8 +51,14 @@ public static void unregisterForPushNotifications(final String senderId) {
4451
new Thread() {
4552
public void run() {
4653
try {
47-
// FirebaseInstanceId.getInstance().deleteToken(senderId, "FCM");
48-
FirebaseInstanceId.getInstance().deleteInstanceId();
54+
// No matter the workflow, on unregister we must ensure that subsequent calls to register
55+
// will trigger any callbacks.
56+
preventInitialRegisterTokenCallback = false;
57+
58+
FirebaseInstanceId.getInstance().deleteToken(senderId, "FCM");
59+
// Do not use deleteInstanceId because by default FCM automatically re-initializes
60+
// the token and we get onPushTokenReceivedCallback triggered.
61+
// FirebaseInstanceId.getInstance().deleteInstanceId();
4962
} catch (IOException e) {
5063
Log.e(TAG, "Error deleting token in FCM: " + e.getMessage(), e);
5164
}
@@ -54,10 +67,17 @@ public void run() {
5467
}
5568

5669
public static void setOnPushTokenReceivedCallback(FirebasePluginListener callbacks) {
70+
// Workflow 1: User uses the registerForPushNotifications
71+
// In this case we are getting a double callback on initial app start up, since the FB instance
72+
// generates a new token at application startup automatically. So we need to prevent
73+
// the one triggered from the register.
74+
// Workflow 2: Users uses addPushTokenReceivedCallback directly
75+
// In this case we need to emit the token
5776
onPushTokenReceivedCallback = callbacks;
5877
if (cachedToken != null) {
5978
executeOnPushTokenReceivedCallback(cachedToken);
6079
cachedToken = null;
80+
preventInitialRegisterTokenCallback = true;
6181
}
6282
}
6383

0 commit comments

Comments
 (0)