FCM令牌什么时候到期?是6个月吗?
答案 0 :(得分:28)
它不会过期。如果发生下列情况之一,它会自动更新。
监控令牌生成
每当生成新令牌时,onTokenRefreshcallback都会触发,所以 在其上下文中调用getToken可确保您访问 当前可用的注册令牌。确保你已经添加了 服务到您的清单,然后在上下文中调用getToken onTokenRefresh,并记录如下所示的值:
@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);
}
答案 1 :(得分:0)
如文档here中所述,令牌不会过期,仅在某些事件上会更改。每当生成新令牌时,都会调用onTokenRefereshId方法。要实现此目的,请创建一个扩展FirebaseInstanceIdService并覆盖onRefreshToken的类,如下所示:
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
@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);
}
}
也不要忘记在清单中注册此服务
<service
android:name=".MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
答案 2 :(得分:0)
使用 firebase admin,您可以执行以下操作:
async function isValidDeviceToken (deviceToken) {
const {
results: [notifResult]
} = await firebaseAdmin.messaging().sendToDevice(
deviceToken,
{
notification: {
title: 'Device Registration',
message: 'Your device has been registered.'
}
},
{
dryRun: true
}
);
// returns true if valid, false if not.
return !notifResult.error;
}
它的作用是检查提供的 deviceToken
是否有效,隐藏在场景中,firebase 管理员检查 deviceToken
是否已注册。如果没有注册,会返回如下错误:
{
error: FirebaseMessagingError: The provided registration token is not registered. A previously valid registration token can be unregistered for a variety of reasons. See the error documentation for more details. Remove this registration token and stop using it to send messages.
at FirebaseMessagingError.FirebaseError [as constructor] (/Users/aprilmintacpineda/projects/my-app/node_modules/firebase-admin/lib/utils/error.js:44:28)
at FirebaseMessagingError.PrefixedFirebaseError [as constructor] (/Users/aprilmintacpineda/projects/my-app/node_modules/firebase-admin/lib/utils/error.js:90:28)
at new FirebaseMessagingError (/Users/aprilmintacpineda/projects/my-app/node_modules/firebase-admin/lib/utils/error.js:256:16)
at Function.FirebaseMessagingError.fromServerError (/Users/aprilmintacpineda/projects/my-app/node_modules/firebase-admin/lib/utils/error.js:289:16)
at /Users/aprilmintacpineda/projects/my-app/node_modules/firebase-admin/lib/messaging/messaging.js:105:63
at Array.forEach (<anonymous>)
at mapRawResponseToDevicesResponse (/Users/aprilmintacpineda/projects/my-app/node_modules/firebase-admin/lib/messaging/messaging.js:101:26)
at /Users/aprilmintacpineda/projects/my-app/node_modules/firebase-admin/lib/messaging/messaging.js:370:24
at processTicksAndRejections (node:internal/process/task_queues:94:5)
at async isValidDeviceToken (/Users/aprilmintacpineda/projects/my-app/test.js:13:7) {
errorInfo: {
code: 'messaging/registration-token-not-registered',
message: 'The provided registration token is not registered. A previously valid registration token can be unregistered for a variety of reasons. See the error documentation for more details. Remove this registration token and stop using it to send messages.'
},
codePrefix: 'messaging'
}
}