We use Firebase Cloud Messaging in our application to display push notifications. According to the FirebaseInstanceId doc, the instance identifier is stable, unless:
- Application removes instance id
- The application is restored on the new device
- User uninstalls / reinstalls the application
- The user deletes the application data.
However, every time we start the application (previously stopped, not resumed), another token is returned via the FirebaseInstanceIdService onTokenRefreshed () callback.
I was wondering if this is the normal behavior of the service or if there is an error in the code.
Update
Dependency in root build gradle file :
classpath 'com.google.gms:google-services:3.0.0'
app build gradle:
"com.google.firebase:firebase-messaging:9.2.1"
"com.google.android.gms:play-services-base:9.2.1"
apply plugin: 'com.google.gms.google-services'
FirebaseInstanceIdService
@Override
public void onTokenRefresh() {
final String oldToken = PrefsHelper.getStringValue(PREF_DEVICE_TOKEN);
Log.d(TAG, "Old token: " + oldToken);
final String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
if (TextUtils.isEmpty(oldToken)) {
handleNewToken(refreshedToken);
} else {
handleTokenUpdate(oldToken, refreshedToken);
}
}