简体   繁体   English

Firebase 云消息 - getMessaging 不是 function

[英]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.尝试使用 firebase 网站中的代码片段从 firebase 中的 node.js 应用程序向 android 设备发送消息。

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

Keep getting getMessaging is not a function.继续获取getMessaging is not a function.

My call is await admin.messaging().getMessaging().send(...)我的电话是await admin.messaging().getMessaging().send(...)

Can't find what is wrong.找不到问题所在。

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:您所指的getMessaging() function 是可以从 firebase firebase-admin/messaging SDK 导入的顶级 function ,如下所示:

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

This is equivalent of admin.messaging() and both return an instance of Messaging class.这相当于admin.messaging()并且都返回Messaging class 的实例。

If you are following Firebase documentation to send messages, the code should be:如果您按照 Firebase 文档发送消息,代码应该是:

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. Admin SDK 不像客户端 SDK 那样完全模块化,因此 rest 的语法保持不变。 Also checkout What is the difference between admin.firestore, admin.firestore() and getFirestore()请查看 admin.firestore、admin.firestore() 和 getFirestore() 之间的区别是什么

You're using firebase-admin sdk which does have different syntax.您使用的 firebase firebase-admin sdk 确实具有不同的语法。 For your use-case, you should instead use:对于您的用例,您应该改为使用:

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

For more information, checkout this documentation .有关更多信息,请查看此文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM