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
For solving this problem I took the following steps:
- open Google Cloud Platform Dashboard
- Go to API and Services
- Enable API and Services
- Search for Cloud Messaging
- Turn on the cloud messaging and Firebase cloud messaging API.
-
14This solved my issue.
Firebase cloud messaging
API was already enabled. I had to just enable thecloud messaging
API. Thanks @Peshraw Hasan Jul 10, 2022 at 4:19 -
1Worked for me too. Didn't know you had to enable regular google cloud messaging in addition to the Firebase Cloud Messaging.– EricJul 15, 2022 at 6:47
-
2Thank 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
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)
-
This worked for me too. I think the interface has changed if you're using the newer API.– d512Aug 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
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
-
1
-
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!!!!
It works After I activate the Cloud Messaging permission.
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);