joseph
joseph

Reputation: 301

Firebase Cloud Messaging - getMessaging is not a function

Trying to send a message from a node.js app in firebase to an android device using code snippets from the firebase website.

https://firebase.google.com/docs/cloud-messaging/android/send-multiple

Keep getting getMessaging is not a function.

My call is await admin.messaging().getMessaging().send(...)

Can't find what is wrong.

Upvotes: 6

Views: 6306

Answers (2)

Dharmaraj
Dharmaraj

Reputation: 50930

The getMessaging() function that you are referring to is a top level function that can be imported from firebase-admin/messaging SDK as shown below:

import { getMessaging } from "firebase-admin/messaging";

This is equivalent of admin.messaging() and both return an instance of Messaging class.

If you are following Firebase documentation to send messages, the code should be:

import { getMessaging } from "firebase-admin/messaging"; // not just "firebase-admin"

const messaging = getMessaging();

await messaging.send(...)

The Admin SDK is not fully modular yet like the the client SDK so rest of the syntax remains same. Also checkout What is the difference between admin.firestore, admin.firestore() and getFirestore()

Upvotes: 15

Marc Anthony B
Marc Anthony B

Reputation: 4099

You're using firebase-admin sdk which does have different syntax. For your use-case, you should instead use:

await admin.messaging().send(...);

For more information, checkout this documentation.

Upvotes: 4

Related Questions