Sebastien
Sebastien

Reputation: 4325

"Requested entity was not found" error when trying to send a push notification using Firebase Cloud Messaging in Firebase Cloud Functions

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.

Upvotes: 69

Views: 138261

Answers (8)

Keshav Suman
Keshav Suman

Reputation: 75

My Solution for this is bit diffrent, I figured it out this error in Nodejs having NestJS as framework with the snippet below.

console.log(e.errorInfo);
if (e.errorInfo.code == "messaging/registration-token-not-registered"){
        notificationService.clearTokenByUserId(deliveryManId)
        console.log(`Deleted Firebase Notification token for the user:${deliveryManId}`)
 }

instead of using "e.errorCode.name" use "e.errorInfo.code".

Upvotes: 0

Ahmad Habib
Ahmad Habib

Reputation: 2382

Hopefully, this will help someone:

I was using Firebase along with my react-native project.

I initially set up my Firebase project using Account A. I used the Firebase project associated with Account A when testing push notifications.

Later, when moving to production, I switched to Account B but mistakenly used the google-services.json file for Android and the GoogleService-Info.plist file for iOS from Account A. Additionally, I registered users in Account B's database using the API keys from Account A.

As a result, I encountered an error when I attempted to send notifications in production. Even after replacing the correct files, I had to remove all previously registered FCM tokens from both accounts to resolve the issue. This was a valuable lesson, and I hope it helps others avoid a similar mistake.

Upvotes: 0

MUHAMMAD AZEEM
MUHAMMAD AZEEM

Reputation: 135

In my case, on the front end (Next.js), multiple tokens were being generated. I fixed the issue of multiple token generation, and the problem was resolved.

Upvotes: 0

Shailendra Madda
Shailendra Madda

Reputation: 21551

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}")
        }
    }
}

Upvotes: 9

Amal
Amal

Reputation: 77

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

Upvotes: 0

Austris Cirulnieks
Austris Cirulnieks

Reputation: 1189

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>

Upvotes: 2

Giles Correia Morton
Giles Correia Morton

Reputation: 872

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.

Upvotes: 4

Sebastien
Sebastien

Reputation: 4325

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.

Upvotes: 92

Related Questions