【问题标题】:TypeError: admin.messaging.sendToDevice is not a function , using firebase functionsTypeError: admin.messaging.sendToDevice is not a function ,使用 firebase 函数
【发布时间】:2020-04-02 08:47:15
【问题描述】:

在使用以下功能时,我正在向设备发送设备通知,我在用于消息传递的 firebase 云功能中收到此错误

TypeError: admin.messaging.sendToDevice 不是函数

return admin.messaging.sendToDevice(to_token_id,payload).then(result=>{

            console.log("Notification Sent Successful");
            return null;
        });

上述方法是否已弃用? 如果是这样,那么新的是什么? 上面的firebase cloud函数是用javascript写的。

【问题讨论】:

  • @PeterHaddad 是的,我检查了你的答案,它很有帮助,但做了一些修改。

标签: javascript firebase google-cloud-firestore google-cloud-functions firebase-cloud-messaging


【解决方案1】:

sendToDevice,需要使用send()的方法:

// The topic name can be optionally prefixed with "/topics/".
var topic = 'highScores';

var message = {
  data: {
    score: '850',
    time: '2:45'
  },
  topic: topic
};

// Send a message to devices subscribed to the provided topic.
admin.messaging().send(message)
  .then((response) => {
    // Response is a message ID string.
    console.log('Successfully sent message:', response);
  })
  .catch((error) => {
    console.log('Error sending message:', error);
  });

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

【讨论】:

    猜你喜欢
    • 2019-10-17
    • 2020-03-01
    • 1970-01-01
    • 2019-02-15
    • 2014-12-30
    • 2019-10-02
    • 2021-11-19
    • 1970-01-01
    相关资源
    最近更新 更多