问题描述
我通过以下方式发送推送通知:
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(event => {
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发送推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!