We are using Firebase Cloud Messaging in our app to displaying push notifications. According to FirebaseInstanceId doc, Instance ID is stable except when:
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.
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);
}
}
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.
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.
exp The time, in seconds, at which the token expires. It can be at a maximum 3600 seconds later than iat.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With