Node.js Firebase应用发消息遇getMessaging is not a function错误
Firebase Cloud Messaging发送消息报错:
getMessaging is not a function 解决方案 你的问题出在多调用了一层getMessaging()方法。Node.js环境下的Firebase Admin SDK里,admin.messaging()本身就已经返回了Messaging服务实例,不需要再链式调用getMessaging()。
正确调用方式
如果是单条消息发送:
await admin.messaging().send(你的消息配置);
如果是批量向多个Android设备发送(对应你参考的批量发送文档),用sendMulticast()方法:
const message = { tokens: ["设备Token1", "设备Token2"], // 替换为实际的设备推送Token notification: { title: "批量通知标题", body: "批量通知内容" }, // 可选:添加数据负载 data: { key1: "value1", key2: "value2" } }; const response = await admin.messaging().sendMulticast(message); console.log(`${response.successCount}条消息发送成功`); console.log(`${response.failureCount}条消息发送失败`);
原因说明
Firebase的客户端SDK(比如Web、Android端)确实需要通过getMessaging()来获取实例,但Admin SDK的设计不同,直接通过admin.messaging()就能拿到可用的Messaging服务对象,不存在getMessaging()这个方法,所以才会报错。
内容的提问来源于stack exchange,提问作者joseph
相关产品推荐
相关产品推荐

