anho
anho

Reputation: 1735

Firebase FCM error: 'InvalidRegistration'

I am currently trying to send a PushNotification to a Device Group using FCM with the help of Firebase Cloud Functions but once the notification is sent, it returns with code 200 but with failure :

SUCCESS response= {
multicast_id: 8834986220110966000,
success: 0,
failure: 1,
canonical_ids: 0,
results: [ { error: 'InvalidRegistration' } ] 
}

Here is the code I am using to send this notification... what am I missing?

const options = {
    method: 'POST',
    uri: 'https://fcm.googleapis.com/fcm/send',
    headers: {
       'Authorization': 'key=' + serverKey,
    },
    body: {
       to: groupId,
       data: {
        subject: message
       },
       notification: {
         title: title,
         body: body,
         badge: 1,
        },
       content_available: true
    },
    json: true
};

return rqstProm(options)
    .then((parsedBody) => {
        console.log('SUCCESS response=', parsedBody);
    })
    .catch((err) => {
        console.log('FAILED err=', err);
    });

Where JSON values title, body, subject, message are String

Upvotes: 32

Views: 60923

Answers (8)

Muhammad Omran
Muhammad Omran

Reputation: 4615

i think you're getting this error because you're testing from localhost, if you build your app it'll work. you can also test via postman

However, Legacy Cloud Messaging API is Deprecated and scheduled to get removed as of June 2024.

So, the solution is to migrate to the new API: HTTP v1 API, which uses OAuth 2.0 Authorization

here is a full documentation on it

POST https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1

Content-Type: application/json
Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA

{
   "message":{
      "token":["bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."],
      "notification":{
        "body":"This is an FCM notification message!",
        "title":"FCM Message"
      }
   }
}

BUT, in order to get this Bearer: whatever token

you're gonna need a server for it anyway, either Google Cloud Function, or Your custom server with Google Admin SDK to generate a token using "Service Account" private json file, you can generate the file from Project settings > Service Accounts and Click Generate New Private Key

and here is how to do that in order to get a token!

SO, you're gonna need a server anyway!

the Easy way is to make a google cloud function to send the notification for you instead and make an http request to it.

and using this quick start sample repo as your function, refer to this answer

Upvotes: 0

Adnan
Adnan

Reputation: 1283

for me, it was a mistake that I was passing an Id from my models instead of the tokens of the users

Upvotes: 0

Mike Schvedov
Mike Schvedov

Reputation: 156

I was losing my mind with this InvalidRegistration error.

Eventually the problem was that I was subscribing my device to "example" but sending the notification json to: "example".

But we actually need to send to "/topics/example"

2 hours of my life wasted..

Upvotes: 4

Ben Butterworth
Ben Butterworth

Reputation: 28532

I was getting InvalidRegistration:

Basic meaning: you are using the wrong token. Why? This may happen when you a new registrationToken is given to you in onNewToken (docs), but for some reason you are using the old token. That could happen when:

  • You're using a different push notification library which remembers token (stores it somewhere locally) and you didn't update that library with the new token.

  • Your application (or other library dependencies) implements another FirebaseMessagingService, and they conflict. Only one service can accept (react to) to the action sent by the FirebaseMessaging Android library's when a new token is given to it. You can double check this by opening the AndroidManifest.xml in Android Studio and selecting the Merged Manifest tab at the bottom of the tab. You can also place debuggers in each Service from each library you use. You'll see that only one service's onNewToken gets called.

    When they conflict, one doesn't get the correct token, and the FCM registration token that gets registered would be wrong. Sending a message to a wrong registration, gets you InvalidRegistration.

Upvotes: 0

Kishan Solanki
Kishan Solanki

Reputation: 14618

InvalidRegistration simply means that the token is either invalid or expired. You can uninstall the app and then reinstall and get a new token and then try with that token. This will definitely solve your problem.

You can read more here.

Upvotes: -2

Code Guru
Code Guru

Reputation: 15578

In my case, I was sending notifications to topic ("topics/my-topic"). I was missing prepending / in the starting of topic so I was getting the same issue. SO topic should be /topics/my-topic.

May be this helps!!

Upvotes: 40

user3824246
user3824246

Reputation: 11

A registration token is tied to a certain group of senders. When a client app registers for FCM, it must specify which senders are allowed to send messages. You should use one of those sender IDs when sending messages to the client app.

Al you need to do is add a http header 'project_id' with your sender id.

Upvotes: 0

Bob Snyder
Bob Snyder

Reputation: 38289

There is an easier way to send a message to a device group from a Cloud Function. Use admin.messaging().sendToDeviceGroup(). Sample code and instructions are in this guide.

I think your current method is failing because there is something wrong with the group notification key provided in groupId. It should be the string key value that was returned when you created the device group. The error codes are listed in this table. For 200/InvalidRegistration it says:

Check the format of the registration token you pass to the server. Make sure it matches the registration token the client app receives from registering with Firebase Notifications. Do not truncate or add additional characters.

Upvotes: 5

Related Questions