每次生成新的Firebase令牌 [英] Firebase token is generated every time new
问题描述
我们在应用程序中使用Firebase Cloud Messaging来显示推送通知.根据 FirebaseInstanceId 文档,实例ID为除以下情况外稳定:
We are using Firebase Cloud Messaging in our app to displaying push notifications. According to FirebaseInstanceId doc, Instance ID is stable except when:
- 应用程序删除实例ID
- 应用已在新设备上还原
- 用户卸载/重新安装应用程序
- 用户清除应用数据
但是,每次我们启动应用程序(以前已停止,未恢复)时,都会通过 FirebaseInstanceIdService onTokenRefreshed()回调返回不同的令牌.
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.
root build gradle 文件中的依赖项:
classpath 'com.google.gms:google-services:3.0.0'
应用构建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);
}
}
推荐答案
我们正在使用 Firebase项目中也有Cloud Messaging .
我们在android应用中使用firebase:
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.
最好确保每次启动应用程序时,令牌是否在您的 FirebaseInstanceService 中刷新:
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);
...
}
}
然后确保在服务器中正确设置了 Firebase服务器 .
Then make sure in your server you have correctly set the Firebase Server.
如果每次启动应用程序时都获得一个不同的令牌,则可能是一个错误.但请确保您已使用最新的Firebase库测试了您的android项目
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.
这个问题可能与您的问题有关:
Firebase Android身份验证失败:expired_token(Auth令牌已过期)
This questions could be related to your problem:
Firebase Android Authentication failed: expired_token (Auth token is expired)
这篇关于每次生成新的Firebase令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!