此events
函数在第一次添加数据时有效,但在添加其他条目时,change.after.val()
将返回所有条目,而不仅仅是新条目。
我可以以某种方式获取所有新条目或我必须fcm/{pushId}/{uid}
,获取每个用户ID并将其发送到sendNotification
吗?
exports.events = functions.database.ref('/fcm/{pushId}')
.onWrite((change, context) => {
// Exit when the data is deleted.
if (!change.after.exists()) {
return null;
}
const eventId = context.params.pushId;
const dsnap = change.after.val();
let guests = Object.keys(dsnap);
const promises = guests.map((data) => admin.database()
.ref('playerIds')
.child(data)
.once('value'));
return Promise.all(promises).then(data => {
return data.map(item => {
let itemVal = Object.keys(item.val());
return itemVal;
})
}).then((itemVal) => {
if (itemVal) {
console.log('itemVal', itemVal)
admin.database().ref(`/events/${eventId}/`)
.once('value')
.then(event => {
if (event) {
let playerIds = [];
for (let id of itemVal) {
playerIds.push(id[0]);
}
let msg = "Your invitation has arrived!";
var eventObject = { "eventKey": eventId, "event": event };
sendNotification(playerIds, msg, eventObject);
}
})
}
});
});
数据库模型:
fcm
Eventid1
UID1: true,
UID2: true
Event2
UID2: true,
...