关于android:Firebase云消息传递:FCM令牌已过期

Firebase Cloud Messaging : Expiration of FCM token

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

-应用程序删除实例ID
-该应用程序已在新设备上还原
-用户卸载/重新安装应用程序
-用户清除应用程序数据。

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

Monitor token generation

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:

1
2
3
4
5
6
7
8
9
10
11
@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服务器无法知道设备的新令牌(如果存在)。因此,当我向该设备发送通知/数据消息时,服务器无法将其发送到该设备,因为它不知道将其发送到哪里(因为没有有效的令牌)。如何确保在这种情况下可以通知设备?我不做客户代币的生成。因此,它似乎不时刷新令牌。如何增加令牌的有效性?


发送消息时,您将需要检查错误,并注意错误代码,如文档中所列。如果出现错误messaging/registration-token-not-registered

,则应停止使用令牌。