【问题标题】:Send multiple firebase cloud messaging on one functions在一个功能上发送多个 Firebase 云消息
【发布时间】: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


    【解决方案1】:

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

    return promisePool.start();
    

    我从来没有使用过承诺池,所以肯定会考虑使用常规的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);
    

    【讨论】:

    • 感谢您的回答,实际上这是我第一次尝试return promisePool.start(); 我认为这就是函数在发送第一条消息后返回的原因,我想知道是否使用 Promise 发送 100 条消息是否无关紧要。全部。
    猜你喜欢
    • 2017-01-25
    • 1970-01-01
    • 2021-09-19
    • 1970-01-01
    • 1970-01-01
    • 2019-02-23
    • 1970-01-01
    相关资源
    最近更新 更多