为什么Firebase Cloud Messaging web客户端消息传递.getToken()有时会产生不同的令牌?



我发现FCM web客户端偶尔会向我提供不同的令牌。

我的代码完全基于官方文档。

// Get registration token. Initially this makes a network call, once retrieved
// subsequent calls to getToken will return from cache.
messaging.getToken({ vapidKey: '<YOUR_PUBLIC_VAPID_KEY_HERE>' }).then((currentToken) => {
if (currentToken) {
// Send the token to your server and update the UI if necessary
// Occasionally, I received a different token here.
} else {
// Show permission request UI
console.log('No registration token available. Request permission to generate one.');
// ...
}
}).catch((err) => {
console.log('An error occurred while retrieving token. ', err);
// ...
});

偶尔,我会收到不同的currentToken;这种情况经常发生!

我没有更改我的web浏览器(Chrome(,也没有更改vapidKey,并且我继续使用相同的选项卡进行localhost开发。

它似乎每隔几个小时就会发生变化。

如果令牌发生更改,我的服务工作者将在控制台中记录一个错误,以及404 API请求失败。

控制台日志错误如下:

FirebaseError: Messaging: A problem occurred while unsubscribing the user from FCM: FirebaseError: Messaging: A problem occurred while unsubscribing the user from FCM: Requested entity was not found. (messaging/token-unsubscribe-failed). (messaging/token-unsubscribe-failed).
at _callee8$ (eval at <anonymous> (app.js:2982), <anonymous>:572:45)
at tryCatch (eval at <anonymous> (app.js:6025), <anonymous>:62:40)
at Generator.invoke [as _invoke] (eval at <anonymous> (app.js:6025), <anonymous>:296:22)
at Generator.prototype.<computed> [as next] (eval at <anonymous> (app.js:6025), <anonymous>:114:21)
at step (eval at <anonymous> (app.js:636), <anonymous>:17:30)
at eval (eval at <anonymous> (app.js:636), <anonymous>:28:13)

这是我认为FCM试图提出的API请求失败;我没有提出这个请求,我将其复制为curl以提高可见性。

curl 'https://fcmregistrations.googleapis.com/v1/projects/chatisfy-d2721/registrations/cD0VOZLBLdaVymfaUbQyE4:APA91bHj2qCU02_Sib6gEPw3VuPTDkjpj0ZVpgmWYaaHESpTjpH-uwY5JX5mn_W7YhJ1AOMp4dNnwpUffs7SQkBs1UYGGie0o4u_i-OjYY5Q5uRSl3pZQoRGVzwNXxe0lDrQIHD4SN5A' 
-X 'DELETE' 
-H 'authority: fcmregistrations.googleapis.com' 
-H 'sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="96", "Google Chrome";v="96"' 
-H 'sec-ch-ua-mobile: ?0' 
-H 'user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36' 
-H 'content-type: application/json' 
-H 'accept: application/json' 
-H 'x-goog-api-key: AIzaSyDFP12b-P9JwiDvJuqsWVz6k2Z8ww6_2-E' 
-H 'x-goog-firebase-installations-auth: FIS eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.eyJhcHBJZCI6IjE6NTI4MTcxMTk2MzYxOndlYjowMWQyMzQ4ODNiYWQ3NWU5MmYxMjE4IiwiZXhwIjoxNjQwMjUwMDc2LCJmaWQiOiJjRDBWT1pMQkxkYVZ5bWZhVWJReUU0IiwicHJvamVjdE51bWJlciI6NTI4MTcxMTk2MzYxfQ.AB2LPV8wRQIhAL4F96JV_fSn2LHzpBiYDWnOVcpA7zBT35lvWz0WqS8fAiAlC28Un0hO2uD6_DuRPnHdqnO_5wIr-byID127niFXRg' 
-H 'sec-ch-ua-platform: "macOS"' 
-H 'origin: http://localhost:8080' 
-H 'x-client-data: CJO2yQEIpbbJAQjBtskBCKmdygEInvnLAQjmhMwBCLWFzAEIy4nMAQjSj8wBGI6eywE=' 
-H 'sec-fetch-site: cross-site' 
-H 'sec-fetch-mode: cors' 
-H 'sec-fetch-dest: empty' 
-H 'referer: http://localhost:8080/' 
-H 'accept-language: en-US,en;q=0.9,zh-TW;q=0.8,zh;q=0.7,zh-CN;q=0.6' 
--compressed

以下是对上述请求的回应:

{
"error": {
"code": 404,
"message": "Requested entity was not found.",
"status": "NOT_FOUND"
}
}

是什么原因造成的?

一些安全模型会过期并每隔一段时间重新发布令牌,以防止令牌的长期有效性。或者令牌可以包含基于请求时间更新的数据。

最新更新