Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Requested entity was not found 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.

like image 352
Sebastien Avatar asked May 20 '19 08:05

Sebastien


People also ask

What is the meaning of requested entity was not found?

If there are, and the message you've received is 'Requested entity was not found' this tells us that the location ID that you used for that location does not match any location that can be found in Google's system.

Can firebase send push notification?

Firebase Cloud Messaging (FCM) provides a reliable and battery-efficient connection between your server and devices that allows you to deliver and receive messages and notifications on iOS, Android, and the web at no cost.

How do I use firebase cloud messaging in node JS?

Before any Firebase cloud messages can be sent using Node. js, an additional JSON file needs to be generated and installed on the server. This file contains a private key that allows Node. js code to communicate through the SDK to the Firebase messaging service and is generated from within the Firebase console.

How do I send messages to Firebase?

Go to Firebase console — →Project Settings — →Cloud Messaging. To send the message select Body — →Raw — →JSON(application/json). You can send Notification Payload , Data Payload and even both using POSTMAN service.


3 Answers

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.

like image 122
Sebastien Avatar answered Oct 06 '22 19:10

Sebastien


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.

like image 40
Giles Correia Morton Avatar answered Oct 06 '22 19:10

Giles Correia Morton


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}")
        }
    }
}
like image 37
Shailendra Madda Avatar answered Oct 06 '22 17:10

Shailendra Madda