Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/470.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从实时数据库查询设备令牌后如何发送Firebase云消息_Javascript_Firebase Realtime Database_Firebase Cloud Messaging_Google Cloud Functions - Fatal编程技术网

Javascript 从实时数据库查询设备令牌后如何发送Firebase云消息

Javascript 从实时数据库查询设备令牌后如何发送Firebase云消息,javascript,firebase-realtime-database,firebase-cloud-messaging,google-cloud-functions,Javascript,Firebase Realtime Database,Firebase Cloud Messaging,Google Cloud Functions,我想在从实时数据库成功查询设备令牌后发送通知 我的留言 let push_token = []; let message = { message: { token: push_token, notification: { title: 'new post', body: title, }, data: { post_id: id, }, }, }; 查询令牌的代码,然后发送通知 admin .database() .ref('/d

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

我的留言

  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 SDK API文档中可以看到,它实际上是一个函数,接受可选的应用程序参数并返回消息传递对象。但是您的代码没有调用方法来获取对象。假设send是消息传递函数的一个属性,但不存在这样的属性

您应该这样使用它:

admin.messaging().send(...)
有关说明正确用法的更多代码示例,请参阅。

admin.messaging().send(message)