diff --git a/plugin.xml b/plugin.xml index b236828ed..231d3b19e 100644 --- a/plugin.xml +++ b/plugin.xml @@ -37,18 +37,12 @@ xmlns:android="http://schemas.android.com/apk/res/android"> - - - - - - diff --git a/src/android/FirebasePlugin.java b/src/android/FirebasePlugin.java index 2ba645835..b3ae9a6a2 100755 --- a/src/android/FirebasePlugin.java +++ b/src/android/FirebasePlugin.java @@ -19,6 +19,7 @@ import com.google.firebase.FirebaseApp; import com.google.firebase.analytics.FirebaseAnalytics; import com.google.firebase.iid.FirebaseInstanceId; +import com.google.firebase.iid.InstanceIdResult; import com.google.firebase.messaging.FirebaseMessaging; import com.google.firebase.remoteconfig.FirebaseRemoteConfig; import com.google.firebase.remoteconfig.FirebaseRemoteConfigInfo; @@ -86,6 +87,18 @@ public void run() { notificationStack.add(extras); } } + + // Fetch initial FCM token + // This used to be done through FirebaseInstanceIdService, which has been deprecated. + // The replacement FirebaseMessagingService.onNewToken is not called for the first token. + FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(new OnSuccessListener() { + @Override + public void onSuccess(InstanceIdResult instanceIdResult) { + String token = instanceIdResult.getToken(); + Log.d(TAG, "FCM Token initialized: " + token); + sendToken(token); + } + }); } }); } diff --git a/src/android/FirebasePluginInstanceIDService.java b/src/android/FirebasePluginInstanceIDService.java deleted file mode 100755 index 329afe522..000000000 --- a/src/android/FirebasePluginInstanceIDService.java +++ /dev/null @@ -1,25 +0,0 @@ -package org.apache.cordova.firebase; - -import android.util.Log; - -import com.google.firebase.iid.FirebaseInstanceId; -import com.google.firebase.iid.FirebaseInstanceIdService; - -public class FirebasePluginInstanceIDService extends FirebaseInstanceIdService { - - private static final String TAG = "FirebasePlugin"; - - /** - * Called if InstanceID token is updated. This may occur if the security of - * the previous token had been compromised. Note that this is called when the InstanceID token - * is initially generated so this is where you would retrieve the token. - */ - @Override - public void onTokenRefresh() { - // Get updated InstanceID token. - String refreshedToken = FirebaseInstanceId.getInstance().getToken(); - Log.d(TAG, "Refreshed token: " + refreshedToken); - - FirebasePlugin.sendToken(refreshedToken); - } -} diff --git a/src/android/FirebasePluginMessagingService.java b/src/android/FirebasePluginMessagingService.java index 4017bedff..44af2ef07 100755 --- a/src/android/FirebasePluginMessagingService.java +++ b/src/android/FirebasePluginMessagingService.java @@ -40,6 +40,19 @@ private String getStringResource(String name) { ); } + /** + * Called when a new token for the default Firebase project is generated. + * + * This is invoked after app install when a token is first generated, and again if the token changes. + * + * @param token The token used for sending messages to this application instance. This token is the same as the one retrieved by getInstanceId(). + */ + @Override + public void onNewToken(String token) { + Log.d(TAG, "New FCM Token: " + token); + FirebasePlugin.sendToken(token); + } + /** * Called when message is received. *