Reputation: 713
I am running a node server with the firebase admin sdk. However, everytime I try to send a push notification from the server, I get a 401 error.
Here's the exact error I'm getting:
errorInfo: {
code: 'messaging/authentication-error',
message: '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. Raw server response: "<HTML>\n' +
'<HEAD>\n' +
'<TITLE>PROJECT_NOT_PERMITTED</TITLE>\n' +
'</HEAD>\n' +
'<BODY BGCOLOR="#FFFFFF" TEXT="#000000">\n' +
'<H1>PROJECT_NOT_PERMITTED</H1>\n' +
'<H2>Error 401</H2>\n' +
'</BODY>\n' +
'</HTML>\n' +
'". Status code: 401.'
},
codePrefix: 'messaging'
I'm not exactly sure why I don't have permissions to the project. I have setup my service account, and downloaded the .json file. I even went into the gcloud platform and tried to add any permission that looked correct. Below are all the permissions associated with my service account:
I am running the server locally, and initialized the app like this:
const admin = require('firebase-admin');
const messaging = require('firebase-admin/messaging');
const serviceAccount = require('<path-to-key>');
const fbApp = admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
projectId: '<PROJECT_ID>',
databaseURL: '<DB_URL>'
});
I am not sure what else to do as I've looked through the v1 documentation multiple times and still don't have any clue as to what permissions I'm lacking. I even made sure to "firebase login" into the correct google account to see if that could've been an issue.
Here's my code to send a message:
const sendPushNotifications2 = async (topic, reminder) => {
const payload = genPayload2(reminder);
//await messaging.getMessaging(fbApp).sendToTopic(topic, payload);
await admin.messaging(fbApp).sendToTopic(topic, payload);
};
I have verified the client_id, client_email, and private_key_id values in the .json file. I haven't yet verified the private_key property because I'm not sure where to find it.
Upvotes: 26
Views: 27726
Reputation: 19
In june, 2024
Firebase removed : Cloud Messaging API (Legacy). you must use Firebase Cloud Messaging API (V1)
you have to generate new key from here : generate from here
Upvotes: 1
Reputation: 91
I had the same issue and since Cloud Messaging API (Legacy) is now deprecated I tried hard to get Firebase Cloud Messaging API (V1) to work.
I got lucky with using getMessaging(app).send() method instead .sendToTopic() or .sendToDevice() because those are using legacy api. More here: https://firebase.google.com/docs/reference/admin/node/firebase-admin.messaging.messaging.md#messaging_class
So in your case, this should work:
import admin from 'firebase-admin';
import { initializeApp } from 'firebase-admin/app'; // I recommend you use initializeApp instead because admin is legacy
import { getMessaging } from 'firebase-admin/messaging';
import serviceAccount from '<path-to-key>';
const fbApp = initializeApp({
credential: admin.credential.cert(serviceAccount),
projectId: '<PROJECT_ID>',
databaseURL: '<DB_URL>'
});
const messaging = getMessaging(fbApp);
const sendPushNotifications2 = async (topic, reminder) => {
// message is defined as [BaseMessage][1] extended by [topic][2] or [token][3] or [condition][4]
const message = {
...reminder, // suppose reminder is BaseMessage
topic: topic
};
await messaging.send(message);
};
Upvotes: 6
Reputation: 32394
The problem is that new Firebase projects have only the new "Firebase Cloud Messaging API (V1)" enabled by default, and with that configuration - the official firebase-admin
NodeJS library - that uses the new V1 API - will not be able to send messages and will get 401 PROJECT_NOT_PERMITTED
errors.
To be able to send message from your server, you MUST also enable the older "legacy" API.
Upvotes: 51
Reputation: 713
It turns out that I had this API disabled: "Firebase In-App Messaging API" It also turns out that this question was asked before, but I wasn't able to find it. Here's the answer
If anyone else runs into this issue this was my fix:
APIs and Services
Enabled APIs & Services
+ Enable APIs and Services
Firebase In-App Messaging API
and make sure it's enabled.You can also just search for messaging and make sure that all the cloud messaging APIs are enabled.
Upvotes: 36