I'm facing an issue working with Firebase Cloud Messaging and the Firebase Admin SDK for Node.JS.
I have the following code, which is supposed to send a firebase notification to an expo app when this route on the express API is called :
const express = require('express');
const router = express.Router();
const admin = require('firebase-admin');
const firebaseServiceAccount = require('../firebase-adminsdk-private-key.json');
/* SEND A NOTIFICATION TO THE DEVICE. */
router.get('/', function (req, res, next) {
const devicePushToken = "here is the device firebase push token";
if (admin.apps.length <= 0) {
admin.initializeApp({
credential: admin.credential.cert(firebaseServiceAccount)
});
}
admin.messaging().sendToDevice(devicePushToken, {
data: {
title: "This is a firebase message",
message: "expo-notifications should be triggered"
}
}).then((response) => {
console.log(response.results);
});
res.send('ok');
});
module.exports = router;
But when I run this code, I get this error :
GET /notify 304 79.005 ms - -
[
{
error: FirebaseMessagingError: The credential used to authenticate this SDK does not have permission to send messages to the device corresponding to the provided registration token. Make sure the credential and registration token both belong to the same Firebase project.
at FirebaseMessagingError.FirebaseError [as constructor] (C:\Users\Enzo\Documents\Alternance\Projects\ITI-Medics-Notifications-Server\node_modules\firebase-admin\lib\utils\error.js:44:28)
at FirebaseMessagingError.PrefixedFirebaseError [as constructor] (C:\Users\Enzo\Documents\Alternance\Projects\ITI-Medics-Notifications-Server\node_modules\firebase-admin\lib\utils\error.js:90:28)
at new FirebaseMessagingError (C:\Users\Enzo\Documents\Alternance\Projects\ITI-Medics-Notifications-Server\node_modules\firebase-admin\lib\utils\error.js:279:16)
at Function.FirebaseMessagingError.fromServerError (C:\Users\Enzo\Documents\Alternance\Projects\ITI-Medics-Notifications-Server\node_modules\firebase-admin\lib\utils\error.js:312:16)
at C:\Users\Enzo\Documents\Alternance\Projects\ITI-Medics-Notifications-Server\node_modules\firebase-admin\lib\messaging\messaging.js:104:63
at Array.forEach (<anonymous>)
at mapRawResponseToDevicesResponse (C:\Users\Enzo\Documents\Alternance\Projects\ITI-Medics-Notifications-Server\node_modules\firebase-admin\lib\messaging\messaging.js:100:26)
at C:\Users\Enzo\Documents\Alternance\Projects\ITI-Medics-Notifications-Server\node_modules\firebase-admin\lib\messaging\messaging.js:363:24
at processTicksAndRejections (node:internal/process/task_queues:96:5) {
errorInfo: [Object],
codePrefix: 'messaging'
}
}
]
The senderId and projectId are the same on both frontend side and backend side. I've tried re-generating my private firebase key for the service account.
This question has been asked here : credentials used to authenticate does not have permission
But no answer have been brought. So I'm trying again...
What am I missing here ?
Thanks in advance for your help