【问题标题】:How to send Firebase cloud messaging after query device token from real time database从实时数据库查询设备令牌后如何发送 Firebase 云消息
【发布时间】:2019-11-26 01:04:46
【问题描述】:

我想在从实时数据库中成功查询设备令牌后发送通知

我的消息

  let push_token = [];
  let message = {
  message: {
  token: push_token,
  notification: {
    title: 'new post',
    body: title,
  },
  data: {
    post_id: id,
   },
  },
 };

查询令牌代码然后发送通知

admin
.database()
.ref('/devices_token')
.once('value', snapshot => {
  snapshot.forEach(function(data) {
    let val = data.val();

    push_token.push(val.fcmToken);
  });
})
.then(() => {
   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);
    });
 });
});

控制台出错

TypeError: admin.messaging.send is not a function at admin.database.ref.once.then

【问题讨论】:

  • admin.messaging().send(message)

标签: javascript firebase-realtime-database firebase-cloud-messaging google-cloud-functions


【解决方案1】:

从 Admin SDK API 文档中可以看到,admin.messaging 实际上是一个函数,它接受一个可选的 App 参数并返回一个 Messaging 对象。但是您的代码没有调用该方法来获取对象。假设 send 是消息传递函数的属性,但不存在这样的属性。

你应该像这样使用它:

admin.messaging().send(...)

另请参阅Firebase Admin documentation 了解更多说明正确用法的代码示例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-21
    • 2020-06-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-04
    • 2021-11-03
    • 2017-03-10
    • 1970-01-01
    相关资源
    最近更新 更多