繁体   English   中英

使用 Firebase 云函数从 firebase 发送推送通知

[英]Send push notification From firebase by using Firebase cloud Functions

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM