首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么Firebase消息传递web客户端messaging.getToken()有时会产生不同的令牌?(相同的浏览器,相同的标签)

为什么Firebase消息传递web客户端messaging.getToken()有时会产生不同的令牌?(相同的浏览器,相同的标签)
EN

Stack Overflow用户
提问于 2021-12-18 19:50:59
回答 1查看 529关注 0票数 0

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

我的代码完全基于官方文件

代码语言:javascript
运行
复制
// 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;这种情况经常发生!

我没有改变我的浏览器(Chrome),也没有改变vapidKey,我继续使用相同的选项卡进行本地主机开发。

好像每隔几个小时就会变一次。

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

控制台日志错误如下:

代码语言:javascript
运行
复制
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)

下面是失败的API请求,我相信FCM试图发出;我没有发出请求,我将其复制为curl,以提高可见度。

代码语言:javascript
运行
复制
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

以下是对上述请求的答复:

代码语言:javascript
运行
复制
{
  "error": {
    "code": 404,
    "message": "Requested entity was not found.",
    "status": "NOT_FOUND"
  }
}

是什么引起的?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-19 01:00:54

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70406620

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档