【问题标题】:Firebase Functions send message to topicFirebase 函数向主题发送消息
【发布时间】:2020-09-30 05:29:17
【问题描述】:

我要创建的是一个简单地向主题发送通知的函数

所以我所做的一切只是发送一个频道名称,而我所拥有的就是这个

exports.sendNotification = functions.https.onRequest((req, res) => {
 
  const dest = req.query.dest;
  
  console.log(dest);
  console.log('Message received');
    

    const payLoad = {
        notification:{
            title: "Message title",
            body: "You have a new message"
        }
    };

    console.log("*** about to send message ");
     admin.messaging().sendToTopic(dest, payLoad).then((response) => {
      console.log("Successfully sent message ", response);
      return res.send(JSON.stringify({"success":"true"}));
  })
  .catch((error) => {
      console.log("Error sending message: ", error);
      return res.send(JSON.stringify({"success":"false"}));
  })
    
});

从我拥有的功能日志中:

11:12:47.622 下午 发送通知 函数执行开始

11:12:47.704 下午 发送通知 9Td4I6aWOoVvNXWNE0pZYjwbXNv1
晚上 11:12:47.705 sendNotification 收到消息
晚上 11:12:47.720 发送通知 函数执行耗时 98 毫秒,完成状态为:'crash'

好像创建payLoad的时候崩溃了,但是我不知道为什么

谢谢

【问题讨论】:

  • 您能否显示您的index.js 文件的全部内容,包括require 语句?

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


【解决方案1】:

我将其发布为答案,因为它对于 comment 部分来说太长了。

请您尝试添加这行代码:

{
  cors(request, response, () => {

正如 thisthis 帖子中的建议?

第一个链接中的发布者似乎收到了与您完全相同的错误,并通过启用 cors 解决了它。你可以找到官方文档here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-23
    • 2021-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-22
    • 1970-01-01
    相关资源
    最近更新 更多