【问题标题】:Send push notification From firebase by using Firebase cloud Functions使用 Firebase 云功能从 firebase 发送推送通知
【发布时间】:2018-06-01 19:19:09
【问题描述】:

我通过以下方式发送推送通知:

exports.sendPushNotification = 
functions.database.ref('/chat_rooms/{id}').onWrite(event => {

console.log(event);
const original = event.data.val();
const reciverId = original['receiverID'];
const text = original['text'];
const senderName = original['senderName'];
var path = 'fcmToken/' + reciverId;

const payload = {
notification: {

title :senderName,
body: text,
badge:'7',
sound:'default',

    }
};
return admin.database().ref(path).once('value').then(allToken => {
    if (allToken.val()){
      var firebaseReceiverID = allToken.val();
      var firebaseToken = firebaseReceiverID['firebaseToken'];

     return admin.messaging().sendToDevice(firebaseToken,payload).then(response => {
     });
      };
   });
 });

我的 firebase 数据库结构是,如下所示: 如果在聊天室下添加了任何孩子,如何发送推送(检测路径)

【问题讨论】:

  • 是否正在发送当前推送通知?你来这里做什么console.log(event);
  • 添加新的聊天室时执行,否则不执行我想在聊天室中添加味精时执行
  • 是的,那是正确的,那么问题是什么?这就是 onWrite 的目的,当在该位置添加任何内容时,它就会被触发。 msg,在数据库中的什么位置?
  • 嗨彼得谢谢,问题是如何获取聊天室下添加的最新消息我细化结构应该是这样的functions.database.ref('/chat_rooms/{PerticularChatRoom_id}/{id }').onWrite(event => { } 这里如何获取添加了 msg 的特定聊天室 id
  • functions.database.ref('/chat_rooms/{PerticularChatRoom_id}/‌​{id}').onWrite(event => { }这里具体的聊天室id如何获取

标签: javascript node.js firebase firebase-realtime-database firebase-cloud-messaging


【解决方案1】:

onWrite是数据库触发器,所以每次在指定位置添加数据都会触发。

现在,如果您想获取 id,您可以这样做:

export.sendPushNotification=functions.database.ref('/chat_rooms/{PerticularChatRoomid}/‌​‌​{id}').onWrite(eve‌​nt => { 

console.log(event);
const chatroomid=event.params.PerticularChatRoomid;

}

这样您就可以从数据库中获取PerticularChatRoomid

要获取通配符{id} 的值,请始终使用event.params

此链接中的更多信息:https://firebase.google.com/docs/functions/database-events

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-18
    • 2021-03-08
    • 2019-02-23
    • 1970-01-01
    • 2020-09-09
    • 1970-01-01
    • 2021-04-04
    • 1970-01-01
    相关资源
    最近更新 更多