We are using Firebase Cloud Messaging in our app to displaying push notifications. According to FirebaseInstanceId doc, Instance ID is stable except when:
- App deletes Instance ID
- App is restored on a new device
- User uninstalls/reinstall the app
- User clears app data
However every time we launch the app (previously stopped, not resumed), a different Token is returned through the FirebaseInstanceIdService onTokenRefreshed() callback.
I was wondering if this is the normal behaviour of the service or there is a bug in the code.
Update
Dependency in root build gradle file:
classpath 'com.google.gms:google-services:3.0.0'
Dependency in app build gradle file:
"com.google.firebase:firebase-messaging:9.2.1"
"com.google.android.gms:play-services-base:9.2.1"
// defined at the bottom of the same file: plugin for firebase
apply plugin: 'com.google.gms.google-services'
FirebaseInstanceIdService:
@Override
public void onTokenRefresh() {
// Get the saved token from the shared preferences
final String oldToken = PrefsHelper.getStringValue(PREF_DEVICE_TOKEN);
Log.d(TAG, "Old token: " + oldToken);
// Get updated InstanceID token.
final String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
if (TextUtils.isEmpty(oldToken)) {
handleNewToken(refreshedToken);
} else {
handleTokenUpdate(oldToken, refreshedToken);
}
}