【问题标题】:Cloud Messaging in Cloud Functions: admin.messagin(...).send is not a functionCloud Functions 中的云消息传递:admin.messaging(...).send 不是函数
【发布时间】:2018-07-31 08:45:40
【问题描述】:

我的函数由数据库事件触发,并使用 Firebase Cloud Messaging 向主题发送通知。我的第一个函数工作正常,但第二个函数一直抛出这个错误:

2018-02-20T21:16:49.878Z E receiveMessage: TypeError: admin.messaging(...).send is not a function
at exports.receiveMessage.functions.database.ref.onCreate (/user_code/index.js:55:27)
at Object.<anonymous> (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:59:27)
at next (native)
at /user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71
at __awaiter (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12)
at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:53:36)
at /var/tmp/worker/worker.js:695:26
at process._tickDomainCallback (internal/process/next_tick.js:135:7)

这里是 index.js 文件:

const functions = require('firebase-functions');

const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
// exports.helloWorld = functions.https.onRequest((request, response) => {
//  response.send("Hello from Firebase!");
// });
exports.recceiveInvitation = /* the function that works */;

exports.receiveMessage = functions.database.ref('/messages/{chatId}/{time}').onCreate((event) => {
    const chatId = event.params.chatId;
    console.log('messages', chatId);
    var sender = event.data.val().name;
    var messageContent = event.data.val().message;
    if(messageContent.length >= 100){
        messageContent = messageContent.substring(0,97)+"...";
    }
    const payload = {
        data: {
            title: `New Message from ${sender}`,
            body: messageContent
        },
        topic: chatId
    };

    return admin.messaging().send(payload);
});

我运行了 npm install firebase-admin,但没有提供帮助。

【问题讨论】:

  • 记录admin.messaging() 的值并用结果更新您的问题。
  • 我应该如何记录它?我试过了: var mesaging = admin.mesaging(); console.log("消息:",messaging);但它甚至没有记录,它只是给了我同样的错误

标签: javascript node.js firebase firebase-cloud-messaging google-cloud-functions


【解决方案1】:
let message = {notification: {title: "your title",body: "your message",},token:"token of user device"
      };

admin.messaging().send(message)

【讨论】:

  • 感谢您提供此代码 sn-p,它可能会提供一些有限的即时帮助。 proper explanation 将通过展示为什么这是解决问题的好方法,并使其对有其他类似问题的未来读者更有用,从而大大提高其长期价值。请编辑您的答案以添加一些解释,包括您所做的假设。
【解决方案2】:

改变这个:

return admin.messaging().send(payload);

到这里:

return admin.messaging().sendToTopic(topicname,payload);

能够向主题发送通知。


你可以做上面的,或者查看下面的注释

注意:

您需要更新firebase-admin npm 包,才能使用send()

npm install firebase-admin@latest

更多信息在这里:-

https://firebase.google.com/support/release-notes/admin/node https://firebase.google.com/docs/cloud-messaging/admin/send-messages

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2018-08-13
  • 1970-01-01
  • 1970-01-01
  • 2017-08-23
  • 1970-01-01
  • 1970-01-01
  • 2017-01-31
  • 2020-11-22
相关资源
最近更新 更多