在使用Firebase Cloud Functions时,尝试发送推送通知时出现了“找不到请求实体”的错误。

51

我正在尝试使用以下代码在Firebase Cloud函数中通过FCM发送多播通知:

const message = {
    tokens: recipients,
    notification: {
        title: title,
        body: body
    },
    data: {
        projectPartnerId: projectPartnerId
    }
};
return admin.messaging().sendMulticast(message);

没有任何推送通知被发送。每个响应都包含相同消息的错误:“未找到请求的实体”。

我在Google Cloud控制台中启用了API(Firebase文档中没有提到,但显然是必要的)。我不知道我还能做什么。而且我找到的所有其他问题都与HTTP API或旧版API相关。我正在使用最新版的Firebase Admin SDK。

6个回答

63

我明白了。所以显然,当我尝试发送的 FCM 令牌不再注册时,就会发生此错误,这可以从 "messaging/registration-token-not-registered" 错误代码看出。在这种情况下,我只需要从用户的令牌中删除此令牌即可。


7
好的,这很有道理。但我要发送到大概十几个代币,那我该如何确定是哪一个代币?我能在发送前对代币进行验证吗? - Dustin Poissant
3
@DustinPoissant 发送带有令牌的消息后,请检查结果。在 Kotlin 中: try { FirebaseMessaging.getInstance().sendAsync(message).get() } catch (e: Exception){ val cause = e.cause if (cause is FirebaseMessagingException) { if (cause.errorCode.name == "NOT_FOUND" || cause.messagingErrorCode.name == "UNREGISTERED") { // 处理无效令牌- 从数据库中删除它 } } } - user1123432
1
你从哪里获取到错误信息的详细含义?我找不到它。 - Eakan Gopalakrishnan

6

如@user1123432所述,我只是按照以下方式操作:

try {

// Logic to send a push notification goes here

 catch (e: Exception) {
    logger.error("Firebase Notification Failed: ${e.message}")
     if (e is FirebaseMessagingException) {
        logger.info("Firebase Notification token for the user: ${user.userName}, errorCodeName: ${e.errorCode.name}, messagingErrorCodeName: ${e.messagingErrorCode.name}")
        if (e.errorCode.name == "INVALID_ARGUMENT" || e.errorCode.name == "NOT_FOUND" || e.messagingErrorCode.name == "UNREGISTERED") {
            myNotificationTokenRepo.clearTokenByUserId(user.uuid)
            logger.info("Deleted Firebase Notification token for the user: ${user.userName}")
        }
    }
}

1

最近在为iOS应用设置推送通知时,遇到了这个问题。我通过阅读GitHub帖子上的此答案,找到了一个成功的解决方法。问题在于,在Info.plist中,FirebaseAppDelegateProxyEnabled被设置为布尔值而不是字符串,因此:

    <key>FirebaseAppDelegateProxyEnabled</key>
    </false>

变成

    <key>FirebaseAppDelegateProxyEnabled</key>
    <string>0</string>

GitHub评论还描述了通过媒体发布实现口味并将Firebase / Messaging添加到Podfile,这与使用Flutter构建iOS应用程序有关。我的项目是使用Flutter构建的,但我们不需要在口味周围实现任何内容或更新Podfile,因为它由Flutter本身管理。

如果在那个文件中找不到<key>FirebaseAppDelegateProxyEnabled</key>怎么办?我应该加上<string>0</string>吗? - Terry Windwalker

0

我曾经遇到过同样的问题,当我重新连接到数据库时,问题得到了解决,请确保所有令牌都处于活动状态并在使用中,并删除未使用的令牌或更新它们。


2
确保所有令牌都处于活动状态并在使用中,删除未使用的令牌或更新它们。问题是在删除之前如何确保这一点? - Shailendra Madda

0

我遇到了同样的错误(不是同样的情况),但如果有人在使用React Native Firebase项目时遇到请求实体未找到和SenderId不匹配的错误,这里有一个对我有效的解决方案。

FCM获取MismatchSenderId


0

在遇到相同问题后,这个方法对我很有帮助。

Info.plist文件中,我做了如下更改:

<key>FirebaseAppDelegateProxyEnabled</key>
<false/>

转换成这个

<key>FirebaseAppDelegateProxyEnabled</key>
<string>NO</string>

Info.plist 存在于哪里? - Shailendra Madda
@ShailendraMadda,它可能有其他名称,例如Ionic项目中的[projectName]-Info.plist。也许您可以搜索Info.plist以查看是否有助于找到它。 - Terry Windwalker

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