本文介绍了使用Firebase Cloud功能从Firebase发送推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am sending push notification by the following way:

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

My firebase database structure is like this: how to send push if any child is added under chatroom(to detect the path)

推荐答案

onWrite 是数据库触发器,因此每次在指定位置添加数据时都会触发该触发器.

onWrite is a database trigger, so everytime you add data under the location that you specify it will be triggered.

现在,如果您想获取ID,可以执行以下操作:

Now if you want to get the id you can do this:

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

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

}

这样,您将能够从数据库中获取 PerticularChatRoomid .

This way you will be able to get the PerticularChatRoomid from the database.

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

To get the values of the wildcards {id}, always use event.params

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

More info in this link: https://firebase.google.com/docs/functions/database-events

这篇关于使用Firebase Cloud功能从Firebase发送推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 02:52