【发布时间】:2018-05-29 18:49:42
【问题描述】:
我有这样的数据:
notification
|----event01
|---- token : "gdTh21dG705ysFA91..."
|---- timestamp : 1513335600000
|---- name : "Name Event A"
|---- etc
|----event02
|---- token : "dG7058J1L8I:APA91..."
|---- timestamp : 1513335600000
|---- name : "Name Event B"
|---- etc
|----event03
|---- token : "dG7058J1L8I:APA91..."
|---- timestamp : 1513355000000
|---- name : "Name Event C"
|---- etc
当timestamp 到来时,我需要用token 向用户发送FCM,会有不止1 个事件具有相同的timestamp 但不同的name,所以我不能只使用数组发送消息令牌。
我尝试这样发送消息,但如果有多个事件具有相同的时间戳,则只发送第一条消息,没有错误。
如何使用一个函数发送所有消息,具有相同时间戳的事件可以是 2、3、4... 或 100。
// Runs Promises in a pool that limits their concurrency.
const promisePool = require('es6-promise-pool');
const PromisePool = promisePool.PromisePool;
// Maximum concurrent message sending.
const MAX_CONCURRENT = 3;
/**
* Send notification to user based on timestamp
* Triggered when /variable/notification node updated
* The node updated by C# service when the event is starting
*/
exports.sendStartNotification = functions.database.ref('/variables/notification').onUpdate(event => {
const epoch = event.data.val();
return admin.database().ref('/notification').orderByChild('timestamp').equalTo(epoch).once('value').then(snapshot => {
// Use a pool so that we send maximum `MAX_CONCURRENT` notification in parallel.
const promisePool = new PromisePool(() => {
snapshot.forEach(childSnapshot => {
let notif = childSnapshot.val();
if (notif.token !== null && notif.token !== undefined && notif.token !== '') {
let payload = {
data: {
key: childSnapshot.key,
title: `Event ${notif.name} started`,
body: `Please check-in`
}
};
// Send the message
return admin.messaging().sendToDevice(notif.token, payload).catch(error => {
console.log("Sending failed:", error);
});
}
});
}, MAX_CONCURRENT);
promisePool.start().then(() => {
console.log(`Sending success ${epoch}`);
});
});
});
【问题讨论】:
标签: android node.js firebase firebase-cloud-messaging google-cloud-functions