Firebase云函数中的消息传递错误

5
在我的项目中,我使用Firebase Functions通过FCM发送消息。我使用了以下API:
admin.messaging().send()

最近,我在调用某些令牌时遇到了以下错误:

Error: Requested entity was not found.
at FirebaseMessagingError.Error (native)
at FirebaseMessagingError.FirebaseError [as constructor] (/user_code/node_modules/firebase-admin/lib/utils/error.js:39:28)
at FirebaseMessagingError.PrefixedFirebaseError [as constructor] (/user_code/node_modules/firebase-admin/lib/utils/error.js:85:28)
at new FirebaseMessagingError (/user_code/node_modules/firebase-admin/lib/utils/error.js:241:16)
at Function.FirebaseMessagingError.fromServerError (/user_code/node_modules/firebase-admin/lib/utils/error.js:271:16)
at /user_code/node_modules/firebase-admin/lib/messaging/messaging-api-request.js:140:50
at process._tickDomainCallback (internal/process/next_tick.js:135:7)

我该怎么解决这个问题? 我确定以前这个功能是可用的,而且没有出现这些错误。
Firebase有做过任何更改吗?


2
请更新您的帖子,展示您如何处理send()返回的Promise以及是否捕获错误。您看到的消息可能是当令牌不再有效(例如应用程序已卸载)时返回的内容。请参阅此相关帖子:https://dev59.com/tVUM5IYBdhLWcg3wYvXl - Bob Snyder
我尝试强制重新获取令牌,但是我仍然遇到同样的错误。 - enfix
@BobSnyder,实际上这是从catch中精确捕获的错误。 - vladkras
1个回答

2
这个错误意味着你尝试发送通知的令牌不存在。
你必须将发送代码添加到try catch块中,并根据错误情况在你的基础上删除(或使令牌无效)。
  try {
    ...
    sendYourNotification(token);
    ...

  } catch (error) {
    if (
      [
        'The registration token is not a valid FCM registration token',
        'Requested entity was not found.',
        'NotRegistered.'
      ].includes(error.message)
    ) {
      // invalidate current token
      // find the user and remove this token
    } else {
      // Log it, because is some really unexpected
    }
  }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接