32

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

6 Answers 6

83

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.
7
  • 14
    This solved my issue. Firebase cloud messaging API was already enabled. I had to just enable the cloud messaging API. Thanks @Peshraw Hasan
    – kamasuPaul
    Jul 10, 2022 at 4:19
  • 1
    Worked for me too. Didn't know you had to enable regular google cloud messaging in addition to the Firebase Cloud Messaging.
    – Eric
    Jul 15, 2022 at 6:47
  • 2
    Thank you so much kamasuPaul. I am surprised that this is not stated in the doc. Even filter with cloud messaging will not show this option. You have to search for cloud messaging at the top, then enable it. After that everything works as expected. Jul 19, 2022 at 20:05
  • Worked for me, Just added Cloud Messaging in to the project. then it worked fine Aug 25, 2022 at 19:00
  • 1
    Firebase should mention this in the Docs, thanks Peshraw!!
    – Zubeir
    Sep 21, 2022 at 19:37
17

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)

2
  • This worked for me too. I think the interface has changed if you're using the newer API.
    – d512
    Aug 28, 2022 at 18:17
  • Thanks a lot. By 2023 API is FCM v1 by default. So following related API is the way to go as pointed by @Abhishek Feb 16 at 12:18
14

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

0
12

Download a new private key and delete the old one!

enter image description here

2
  • 1
    I was wondering how to get to the screen in the image. I finally figured it out following the doc in the link
    – Dipen
    Feb 17, 2022 at 16:36
  • How do you use this key in the code?
    – Zorayr
    Jun 25, 2022 at 19:55
8

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.

2
  • This one worked
    – Rajesh
    May 3 at 10:16
  • But this is the legacy version, which firebase recommended to not use.
    – Junius L
    Sep 2 at 9:42
2

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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.