28

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

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
UA DEV
  • 391
  • 1
  • 3
  • 8

6 Answers6

78

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.
Peshraw Hasan
  • 1,060
  • 1
  • 8
  • 14
  • 13
    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 '22 at 04: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 '22 at 06: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. – function1983 Jul 19 '22 at 20:05
  • Worked for me, Just added Cloud Messaging in to the project. then it worked fine – Chinthaka Dinadasa Aug 25 '22 at 19:00
  • 1
    Firebase should mention this in the Docs, thanks Peshraw!! – Zubeir Sep 21 '22 at 19:37
  • firebase cloud messaging already enable in my console still its shows this error. my code is as follows: `admin.messaging().sendToDevice(deviceToken, payload).then((resp) => { console.log('response in', resp); })`. and the device token i have given is the fcm token of the device – Akash Sep 22 '22 at 04:09
  • why google just activated firebase messaging rather than just enabling both – Clode Morales Pampanga III May 06 '23 at 01:04
16

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)

Abhishek Ghimire
  • 1,826
  • 2
  • 8
  • 11
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

Guillaume Munsch
  • 1,233
  • 17
  • 37
11

Download a new private key and delete the old one!

enter image description here

Abhimanyu
  • 11,351
  • 7
  • 51
  • 121
UA DEV
  • 391
  • 1
  • 3
  • 8
  • 1
    I was wondering how to get to the screen in the image. I finally figured it out following the doc in the [link](https://firebase.google.com/docs/admin/setup) – Dipen Feb 17 '22 at 16:36
  • How do you use this key in the code? – Zorayr Jun 25 '22 at 19:55
7

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.

John
  • 135
  • 1
  • 4
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);
Stéphane de Luca
  • 12,745
  • 9
  • 57
  • 95