UA DEV
UA DEV

Reputation: 491

An error occurred when trying to authenticate to the FCM servers

An error occurred when trying to authenticate to the FCM servers. Make sure the credential used to authenticate this SDK has the proper permissions. See https://firebase.google.com/docs/admin/setup for setup instructions enter image description here

Upvotes: 37

Views: 19324

Answers (6)

Stéphane de Luca
Stéphane de Luca

Reputation: 13611

Just to clarify the FCM v1 usage as pointed by @Abhishek Ghimire.

Here is how to create the message:

  const message = {
      token: registrationToken,
      notification: {
        title: "Notification Title",
        body: "Notification Body ",
      },  
  };

  admin.messaging().send(message);

Upvotes: 2

John
John

Reputation: 165

In my case, following Abhimanyu's solution to generate a new key still couldn't solve the problem.

After digging for 5 hours, I found the root cause that Google didn't activate this API for my project!!!!

enter image description here

It works After I activate the Cloud Messaging permission.

Upvotes: 10

Abhishek Ghimire
Abhishek Ghimire

Reputation: 2434

By default Firebase uses Firebase Cloud Messaging API (V1) so you need to use this api instead of legacy one

for Firebase Cloud Messaging API (V1) :

const data = {
    message: {
      token: registrationToken,
      notification: {
        title: "Notification Title",
        body: "Notification Body ",
      },
      data: {
        Nick: "Mario",
        Room: "PortugalVSDenmark",
      },
    },
  };

  admin.messaging().send(data.message);

for Cloud Messaging API (Legacy) 1.First go to Google Cloud Platform Dashboard and enable Cloud Messaging Service 2. and then you can use like this :

 var payload = {
    notification: {
      title: "This is a Notification",
      body: "This is the body of the notification message.",
    },
  };

  var options = {
    priority: "high",
  };

  var registrationToken ="your device token";
   admin
     .messaging()
     .sendToDevice(registrationToken, payload, options)
    .then(function (response) {
       console.log("Successfully sent message:", response);
     })
     .catch(function (error) {
      console.log("Error sending message:", error);
     });

Firebase Cloud Messaging API (V1) is recommended than Cloud Messaging API (Legacy)

Upvotes: 19

Peshraw Hasan
Peshraw Hasan

Reputation: 1270

For solving this problem I took the following steps:

  1. open Google Cloud Platform Dashboard
  2. Go to API and Services
  3. Enable API and Services
  4. Search for Cloud Messaging
  5. Turn on the cloud messaging and Firebase cloud messaging API.

Upvotes: 98

UA DEV
UA DEV

Reputation: 491

Download a new private key and delete the old one!

enter image description here

Upvotes: 12

Guillaume Munsch
Guillaume Munsch

Reputation: 1283

I just came across this issue and, for me, it was because I was using a simulator. The FCM token generated by the simulator isn't valid for firebase cloud messaging and it was throwing this error.

I put my admin.messaging().sendToDevice() into a try catch and it's working fine.

I don't know if is sends the notification only to the valid tokens or if it totally ignores all of them though

Upvotes: 14

Related Questions