Firebase Cloud Messaging:FCM令牌已过期 [英] Firebase Cloud Messaging : Expiration of FCM token

查看:79
本文介绍了Firebase Cloud Messaging:FCM令牌已过期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解,如果发生以下情况之一,FCM令牌会自动更新.

I understand that the FCM token renews itself if one of the following happens.

-该应用程序删除实例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.

以下内容可在应用程序端用于监视令牌更新.

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 ?

推荐答案

发送消息时,您将需要检查错误,并注意错误代码,例如

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 Cloud Messaging:FCM令牌已过期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆