[英]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 => {
});
};
});
});
onWrite
是一个数据库触发器,所以每次你在你指定的位置下添加数据时都会被触发。
现在,如果您想获取 id,您可以这样做:
export.sendPushNotification=functions.database.ref('/chat_rooms/{PerticularChatRoomid}/{id}').onWrite(event => {
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.