如何使用Firebase云功能将通知发送到多个设备令牌 [英] How to send notification to multiple device token using firebase cloud functions
问题描述
我是Firebase Cloud Functions和JavaScript的新手,并且能够使用Firebase Cloud Functions(JavaScript)将通知发送到单个设备.现在,我尝试将推送通知发送到多个设备令牌,但我认为这有问题.
I am new in using Firebase Cloud Functions and JavaScript and I was able to send notification to a single device using Firebase Cloud Functions(JavaScript). Now I am trying to send push notification to multiple device token and I think I have a problem on it.
我想将通知发送到我的Firebase数据库中的这些设备令牌:
I want to send the notification to these device tokens in my firebase database:
/receivers
/event1
/uid1: device_token1
/uid2: device_token2
/uid3: device_token3
/uid4: device_token4
到目前为止,这是我的代码,但是无法正常工作.
This is my code so far but it doesn't work..
exports.sendSOSNotif = functions.database.ref('/SOSNotifs/{sosId}').onWrite((data, context) => {
const eventId=data.after.child("eventId").val();
const uid=data.after.child("uid").val();
const displayName=data.after.child("displayName").val();
const photoUrl=data.after.child("photoUrl").val();
const status=data.after.child("status").val();
console.log("eventId:", eventId);
console.log("displayName:", displayName);
console.log("uid", uid);
const payload = {
notification: {
title: "SOS Alert",
body: displayName + " sent an alert!",
sound: "default"
},
data: {
eventId: eventId,
displayName: displayName
}
};
return Promise.all([admin.database().ref("/receivers/event1").once('value')]).then(results => {
const tokens = results[0];
if (!tokens.hasChildren()) return null;
const tokensList = Object.keys(tokens.val());
return admin.messaging().sendToDevice(tokensList, payload);
});
});
推荐答案
首先,如果这是组织数据库的方式,则不应添加如下所示的令牌.单个uid可能有多个令牌
/receivers
/event1
/uid1: device_token1
/uid2: device_token2
/uid3: device_token3
/uid4: device_token4
And for sending notifications to multiple UIDs, I've written a script here
此外,请更新您所面临的问题的问题.
Also, update your question about what exactly the problem you are facing.
这篇关于如何使用Firebase云功能将通知发送到多个设备令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!