问题描述
我了解,如果发生以下任一情况,FCM 令牌会自行更新.
I understand that the FCM token renews itself if one of the following happens.
-应用删除Instance ID
- 应用程序在新设备上恢复
- 用户卸载/重新安装应用程序
- 用户清除应用数据.
-The app deletes Instance ID
-The app is restored on a new device
-The user uninstalls/reinstall the app
-The user clears app data.
App端可以使用以下方式监控Token更新.
The following can be used at the App side to monitor Token renewal.
监控令牌生成
每当生成新令牌时都会触发 onTokenRefreshcallback,因此在其上下文中调用 getToken 可确保您正在访问一个当前可用的注册令牌.确保您已添加服务到您的清单,然后在上下文中调用 getTokenonTokenRefresh,并记录如下所示的值:
The onTokenRefreshcallback fires whenever a new token is generated, so calling getToken in its context ensures that you are accessing a current, available registration token. Make sure you have added the service to your manifest, then call getToken in the context of onTokenRefresh, and log the value as shown:
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
// If you want to send messages to this application instance or
// manage this apps subscriptions on the server side, send the
// Instance ID token to your app server.
sendRegistrationToServer(refreshedToken);
}
我的问题是,当应用程序终止时,令牌过期并且 FCM 服务器无法知道设备的新令牌是什么(如果存在).因此,当我向此设备发送通知/数据消息时,服务器无法将其发送到设备,因为它不知道将其发送到哪里(因为没有有效的令牌).如何确保在这种情况下我可以通知设备?我不进行客户令牌生成.所以它似乎不时刷新令牌.如何提高我的令牌的有效性?
My question is, when the app is terminated, the token expires and there is no way for the FCM server to know what the new token is for the device (if exist). So when I send a notification/data message to this device, the server fails to send it to the device as it doesn't know where to send it to (as there is no valid token). How do I make sure that in such situations I can notify the device ? I dont do a customer token generation. So the it seems to refresh the token now and then. How do I increase the validity of my token ?
推荐答案
发送消息时需要检查错误,注意错误码,如在文档中列出.如果收到错误 messaging/registration-token-not-registered
,则应停止使用令牌.
You will need to check for an error when sending the message, and pay attention to the error codes, as listed in the documentation. You should stop using the token if you get the error messaging/registration-token-not-registered
.
这篇关于Firebase 云消息传递:FCM 令牌过期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!