Firebase ID is generated every time a new

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"

// 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);
  }
}
+4
1

Firebase Cloud Messaging .

firebase Android

compile 'com.google.firebase:firebase-core:9.0.2'

, , , .

, FirebaseInstanceService:

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {

  private static final String TAG = "MyFirebaseIIDService";

  @Override public void onTokenRefresh() {
    ...

    // Get updated InstanceID token.
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    // check here.
    Log.d(TAG, "Refreshed token: " + refreshedToken);

    ...
  }
}

, Firebase Server.

, . , Android Firebase.

:
Firebase Android: expired_token ( )

+3

Source: https://habr.com/ru/post/1648680/


All Articles