Kenneth Argo
Kenneth Argo

Reputation: 1767

How can I use FCM from within Firebase function

I'm trying to send a FCM from within a firebase function but I'm getting the following error every time:

await admin.messaging().sendToDevice(registrationToken, message)

I followed the suggestions from this post, of the same topic, no help. Pointing to Add the Firebase Admin SDK to your server, another link in the article, isn't very helpful as I am using Firebase Server.

This article, same, Sending FCM using Firebase Functions and I'm using the same code (the FCM code), same error.

This is the full error:

Error sending message: { 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. Raw server response: "<HTML>
<HEAD>
<TITLE>Unauthorized</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Unauthorized</H1>
<H2>Error 401</H2>
</BODY>
</HTML>
". Status code: 401.
    at FirebaseMessagingError.FirebaseError [as constructor] (/workspace/node_modules/firebase-admin/lib/utils/error.js:43:28)
    at FirebaseMessagingError.PrefixedFirebaseError [as constructor] (/workspace/node_modules/firebase-admin/lib/utils/error.js:89:28)
    at new FirebaseMessagingError (/workspace/node_modules/firebase-admin/lib/utils/error.js:255:16)
    at Object.createFirebaseError (/workspace/node_modules/firebase-admin/lib/messaging/messaging-errors-internal.js:57:12)
    at /workspace/node_modules/firebase-admin/lib/messaging/messaging-api-request-internal.js:78:51
    at process._tickCallback (internal/process/next_tick.js:68:7)
  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>Unauthorized</TITLE>\n</HEAD>\n<BODY BGCOLOR="#FFFFFF" TEXT="#000000">\n<H1>Unauthorized</H1>\n<H2>Error 401</H2>\n</BODY>\n</HTML>\n". Status code: 401.' },
  codePrefix: 'messaging' } 

Upvotes: 3

Views: 1908

Answers (3)

Ahmed Elshazly
Ahmed Elshazly

Reputation: 56

This worked for me try it: 1- Go to google cloud. 2- then go to API and Services. 3- then Enabled API and Services. 4- then turn on the cloud messaging and Turn on Firebase cloud messaging API.

Upvotes: 1

Marc Soler
Marc Soler

Reputation: 123

How I solved it:

  1. Go to Google Service Account and select your project.
  2. It will display a list of accounts. Click on the account something@appspot.gserviceaccount.com
  3. Select tab "Keys". Generate a new key and download it as a JSON.
  4. Lastly, add the generated JSON to your projects using the function .cert():

This is only for testing, remember to remove the credentials when you deploy your functions!

admin.initializeApp({
  credential: admin.credential.cert(
    "route/to/generatedJSON.json"
  ),
});
const messaging = admin.messaging();

Upvotes: 5

Kenneth Argo
Kenneth Argo

Reputation: 1767

OK, unrelated but related I found the answer...

gcp-the-caller-does-not-have-a-permission

Basically, follow these instructions to add

Firebase Admin

to your .gserviceaccount.com account.

Upvotes: 1

Related Questions