Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase token is generated every time new

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);
  }
}
like image 515
0xPixelfrost Avatar asked Jul 21 '16 16:07

0xPixelfrost


People also ask

How are Firebase tokens generated?

Firebase gives you complete control over authentication by allowing you to authenticate users or devices using secure JSON Web Tokens (JWTs). You generate these tokens on your server, pass them back to a client device, and then use them to authenticate via the signInWithCustomToken() method.

How often do FCM tokens change?

To cover all cases, you should adopt a threshold for when you consider tokens stale; our recommendation is two months. Any token older than two months is likely to be an inactive device; an active device would have otherwise refreshed its token.

How long does a Firebase token last?

exp The time, in seconds, at which the token expires. It can be at a maximum 3600 seconds later than iat.

Does Firebase refresh token expire?

Firebase ID tokens are short lived and last for an hour; the refresh token can be used to retrieve new ID tokens. Refresh tokens expire only when one of the following occurs: The user is deleted. The user is disabled.


1 Answers

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)

like image 196
ישו אוהב אותך Avatar answered Sep 24 '22 05:09

ישו אוהב אותך