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

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数据库结构如下所示:
    如果在聊天室中添加了任何孩子,如何发送推送(检测路径)

最佳答案

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

10-08 19:43