繁体   English   中英

在一个功能上发送多个firebase云消息

[英]Send multiple firebase cloud messaging on one functions

我有这样的数据:

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,将有多个具有相同timestampname不同的事件,因此我不能仅使用令牌数组发送消息。

我尝试发送这样的消息,但如果有多个事件具有相同的时间戳,则只发送第一条消息,没有错误。

如何使用一个函数发送所有消息,具有相同时间戳的事件可以是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}`);
        });
    });
});

您没有在代码末尾返回承诺,这意味着它可能随时终止(或继续运行超过必要的时间)。

return promisePool.start();

我从来没有使用过promise池,所以肯定会考虑使用常规的Promise.all()来查看是否会产生影响:

var promises = [];
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
        promises.push(admin.messaging().sendToDevice(notif.token, payload));
    }
});
return Promise.all(promises);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM