7

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);
  }
}
Community
  • 1
  • 1
0xPixelfrost
  • 10,244
  • 5
  • 39
  • 58
  • 2
    This is definitely not the expected behaviour, you generally have the same token till the app is uninstalled and reinstalled. Are you getting the onTokenRefresh callback at all, which is called when the apps token is changed? – Arthur Thompson Jul 22 '16 at 15:58
  • Yes, everytime we start the app (quit before, not resumed) we got a new token through the service's callback onTokenRefresh(). I updated the question – 0xPixelfrost Jul 25 '16 at 08:18
  • Did you use `compile 'com.google.firebase:firebase-core:9.2.1'` ? – ישו אוהב אותך Jul 25 '16 at 08:35
  • What device are you seeing this on? What version of Google Play services is the device running? Have you been able to replicate this on an emulator with Google APIs? – Arthur Thompson Jul 25 '16 at 16:09
  • 1
    Found the reason: The app was cleaning too much data on restart and also the saved token, which was saved inside the apps internal storage. Thanks for helping and sorry for that stupid bug – 0xPixelfrost Jul 26 '16 at 09:42
  • 1
    Glad that you've solved your problems. Everyone sometimes makes a mistake. Thank you for teach us from your experience. :). – ישו אוהב אותך Jul 27 '16 at 01:12

1 Answers1

6

We are using Firebase Cloud Messaging in a project too.

We use firebase in android app with:

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

The token we receipt persist whenever we relaunch the app.

It's better to make sure if each time you launch the app, token get refreshed in your 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);

    ...
  }
}

Then make sure in your server you have correctly set the Firebase Server.

It could be a bug if you getting a different token every time you launching your app. But make sure you have tested your android project with the newest Firebase Library.

This questions could be related to your problem:
Firebase Android Authentication failed: expired_token (Auth token is expired)

Community
  • 1
  • 1
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
  • We are currently using the latest version com.google.firebase:firebase-messaging:9.2.1. It's just about token generation, so the server can't be an issue here. We only have the two services FirebaseMessagingService and FirebaseInstanceIdService in our app, no additional code somewhere else. Based on your snipped we would run into onTokenRefresh() with a new token everytime we terminate the app – 0xPixelfrost Jul 22 '16 at 07:56
  • 1
    I've tried using firebase 9.2.1 and it works correctly. The token still persists. as @ArthurThompson says, which code is called when the apps token is changed? – ישו אוהב אותך Jul 23 '16 at 01:42
  • onTokenRefresh is deprecated now – Shwarz Andrei Jul 25 '18 at 12:03