Javascript 如何使用firebase云函数向多个设备令牌发送通知
我在使用Firebase云函数和JavaScript方面是新手,我能够使用Firebase云函数Java脚本向单个设备发送通知。现在我正在尝试向多个设备令牌发送推送通知,我想我遇到了一个问题 我想向firebase数据库中的这些设备令牌发送通知:Javascript 如何使用firebase云函数向多个设备令牌发送通知,javascript,firebase,firebase-realtime-database,firebase-cloud-messaging,google-cloud-functions,Javascript,Firebase,Firebase Realtime Database,Firebase Cloud Messaging,Google Cloud Functions,我在使用Firebase云函数和JavaScript方面是新手,我能够使用Firebase云函数Java脚本向单个设备发送通知。现在我正在尝试向多个设备令牌发送推送通知,我想我遇到了一个问题 我想向firebase数据库中的这些设备令牌发送通知: /receivers /event1 /uid1: device_token1 /uid2: device_token2 /uid3: device_token3
/receivers
/event1
/uid1: device_token1
/uid2: device_token2
/uid3: device_token3
/uid4: device_token4
到目前为止,这是我的代码,但不起作用
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可能有多个令牌
为了向多个UID发送通知,我编写了一个脚本
此外,更新您的问题,了解您面临的问题到底是什么 那么,存储代币的最佳方式是什么?
/receivers
/event1
/uid1: device_token1
/uid2: device_token2
/uid3: device_token3
/uid4: device_token4