在使用 Firebase 时,我的 android 应用程序遇到了这个严重的问题。 1. 我的应用程序在第一次启动时收到 token 2. 我能够从 firebase 控制台向注册的 token 发送通知 3. 如果我在第 2 步之后尝试再次使用控制台发送通知。它在第 2 次尝试之后显示“未注册的 token ”。
我已经在 list 中进行了所有必要的配置,并且 google service.json 文件也具有正确的配置。我相信事情是正确的,因为该应用能够收到一次通知,然后问题才开始出现。
更新 1: 如果我卸载应用程序并重新安装它,那么我也只能收到一次通知。
想看代码的人,我是这样获取token的:
@Override
public void onTokenRefresh() {
//Getting registration token
refreshedToken = FirebaseInstanceId.getInstance().getToken();
//Displaying token on logcat
Log.d(TAG, "Refreshed token: " + refreshedToken);
saveDeviceToken(refreshedToken);
}
这只会在第一次启动时被调用,之后我没有看到它被调用(我认为这是预期的行为)。
收到的 OnMessage 也会在第一次通知时被调用,然后它永远不会被取消:
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d("FCM", "From: " + remoteMessage.getFrom());
if (remoteMessage.getNotification() != null) {
Log.d("FCM", "Notification Message Body: " + remoteMessage.getNotification().getBody());
sendNotification(remoteMessage.getNotification().getBody());
}
}
更新 2: 尝试使用相同的服务器 key 和 token 访问 FCM 的 HTTP api 并得到以下响应:
{
"multicast_id": 6286279702096230688,
"success": 0,
"failure": 1,
"canonical_ids": 0,
"results": [
{
"error": "NotRegistered"
}
]
}
为了避免交叉问题,这里有更多细节:
- Android 工作室:v2.3.1
- 谷歌播放服务版本:10.0.1
- 包括的库:核心、数据库、存储、消息传递 - 都与 google play service 10.0.1 具有相同的版本
更新 3:Firebase 崩溃,数据库和存储在同一个项目中工作(这表明 google service.json 文件是正确的)。
请帮我把它修好。
最佳答案
我记得以前遇到过类似的问题。这是我所做的,它对我来说工作顺利。希望这会有所帮助,
PreferenceStore 是一个帮助类,用于保存、编辑和删除共享首选项。
@Override
public void onTokenRefresh() {
String token = FirebaseInstanceId.getInstance().getToken();
Log.i(TAG, "New FCM Token: " + token);
setup_flag = PreferenceStore.getBoolean(getApplicationContext(), PrefKeys.IS_FIRST_FCM_TOKEN);
if (!setup_flag) {
// save token and boolean value in shared preferences for
// the first time you run the app after install
PreferenceStore.saveString(getApplicationContext(), "FCM_TOKEN", token);
PreferenceStore.saveBoolean(getApplicationContext(), "IS_FIRST_FCM_TOKEN", true);
} else {
// other time your app loads, update the token (like call the API endpoint).
}
}
关于android - 发送一个通知后未注册的 token 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43531925/