Requested entity was not found when trying to send a push notification using Firebase Cloud Messaging in Firebase Cloud Functions
Asked Answered
B

5

55

I'm trying to send a multicast notification via FCM from a Firebase Cloud function with the following code:

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

And none of the push notifications is getting sent. Each response contains an error with the same message: "Requested entity was not found".

I enabled the API in the Google Cloud console (which was not mentioned anywhere in the Firebase documentation but apparently that was necessary). I don't know what else I can do. And all the other questions I could find related to the HTTP API or the legacy API. I'm using the latest version of the Firebase Admin SDK.

Betts answered 20/5, 2019 at 8:22 Comment(0)
B
70

Figured it out. So apparently, this error happens when the FCM token I'm trying to send to is not registered anymore, as evidenced by the "messaging/registration-token-not-registered" error code. In that case I just need to remove this token from the user's token and be done with it.

Betts answered 20/5, 2019 at 9:37 Comment(3)
Ok that makes sense, but I am sending to maybe a dozen tokens, so how do i figure out which one it is, can i somehow validate a token before sending?Cirrose
@DustinPoissant check for result after sending message using some token. In 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") { // handle invalid token- remove it from DB } } }Remit
where did you get the details of what the error message meant? I couldn't find it.Perri
A
7

As stated by @user1123432 I just did like below:

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}")
        }
    }
}
Alternately answered 21/12, 2021 at 6:6 Comment(0)
L
1

I recently ran into this issue when setting up push notifications for an iOS app. I found a successful fix by following a fix posted on a GitHub thread via this answer. The issue was that in the Info.plist FirebaseAppDelegateProxyEnabled was set to bool rather than a string so:

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

becomes

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

The GitHub comment also describes implementing flavours via a medium post and adding Firebase/Messaging to the Podfile, this is related to using Flutter to build an iOS app. My project is built with Flutter but we didn't need to implementing anything around flavours or update the Podfile as it's managed by Flutter itself.

Lollygag answered 17/7, 2020 at 11:53 Comment(1)
What if <key>FirebaseAppDelegateProxyEnabled</key> is not in that file? Should I add it with <string>0</string>?Dissociable
A
0

After facing the same issue, this did the trick for me.

In the Info.plist file I changed this

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

into this

<key>FirebaseAppDelegateProxyEnabled</key>
<string>NO</string>
Alixaliza answered 24/8, 2020 at 11:15 Comment(2)
Where is the Info.plist exists?Alternately
@ShailendraMadda It could be in other names, like [projectName]-Info.plist in the case of Ionic project. Perhaps you can search for Info.plist to see if there's something can help you find it.Dissociable
A
0

I have faced the same issue and it was resolved when I reconnected to the database, make sure all tokens are active and in use and delete the unused once or update them

Acorn answered 15/9, 2021 at 11:27 Comment(1)
` make sure all tokens are active and in use and delete the unused once or update them` The question here is how did you make sure this before deleting it?Alternately

© 2022 - 2024 — McMap. All rights reserved.