I'm trying to send a message to a topic with FCM in a Firebase Cloud Function triggered when a Firestore document (a message) is created. Subscribing to the topic (also done with Functions) and triggering the send function works fine, but the actual send fails with:
Error: 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.
and some raw HTML containing <H1>PROJECT_NOT_PERMITTED</H1>
and <H1>PROJECT_NOT_PERMITTED</H1>
.
Here is my code (index.ts):
import * as admin from 'firebase-admin';
admin.initializeApp({
credential: admin.credential.applicationDefault(),
});
export * from './messages';
and (messages.ts):
import * as admin from 'firebase-admin';
import * as functions from 'firebase-functions';
export const publishMessage = functions
.firestore.document('/messages/{messageId}').onCreate(
(snapshot, context) => {
const data = snapshot.data();
const message = {
notification: {
title: `${data.sentBy} sent a message`,
body: data.message,
},
};
return admin.messaging().sendToTopic('messages', message);
},
);
According to https://firebase.google.com/docs/cloud-messaging/auth-server#provide-credentials-using-adc this should work. I have also tried doing it without any parameters (https://firebase.google.com/docs/admin/setup#initialize-without-parameters) but it fails all the same. What am I missing?
service account
? – Kloster<projectId>@appspot.gserviceaccount.com
. I've posted an answer to further discuss this. – Kloster