【问题标题】:Cloud Functions for Firebase send push to multiple usersCloud Functions for Firebase 向多个用户发送推送
【发布时间】:2018-02-22 10:55:03
【问题描述】:

我尝试让多个推送通知在 Cloud Functions for Firebase 上运行,但没有成功。

我将我的消息回执存储在一个节点中

  • message_receipts
    • -KtvyTN3nbVKoFjHdpJg
    • hHhs5Aco38X1W8EhaaxrwsQDXwy1:"收据"
    • nI25FjUnBfQiCWzdCUIAe8CWTPQ2:"收据"

要发送推送通知,我尝试使用云功能:

//*********************************************************************************************************** */
//handle lsit item added by shared user
if (String(msgData.messageType) == 'ListItemAddedBySharedUser') {

    return admin.database().ref("message_receipts").child(event.params.messageID).once('value').then(receipts => {

        receipts.forEach(function (receipt) {

            //Send push to receipt
            return admin.database().ref('/users/' + receipt.key).once('value').then(usnap => {

                //Send push to users fcmToken
                const userSnap = usnap.val()
                console.log('sending Push to ' + userSnap.fcmToken)


                //create Notification Payload
                var payload = {
                    notification: {
                        title: msgData.title,
                        body: msgData.message,
                        badge: '1',
                        sound: 'default',
                        sbID: String(event.data.key),
                        senderID: msgData.senderID,
                        listID: msgData.listID,
                        receiptID: receipt.key,
                        notificationType: String(msgData.messageType),
                    }
                };

                return admin.messaging().sendToDevice(userSnap.fcmToken, payload).then(response => {

                    console.log("Successfully sent invite message:", response)
                    console.log(response.results[0].error)

                }).catch((err) => { console.log("Error sending Push", err) })

            })

        })

    })
} //*********************************************************************************************************** */

我得到的只是发送了一个通知。 我对 java 脚本和云函数很陌生。 我需要做什么才能通知我的所有用户?

【问题讨论】:

  • 您好,您介意发布您完成的解决方案吗?很想看看。

标签: firebase firebase-cloud-messaging google-cloud-functions


【解决方案1】:

您可以按照文档中的说明发送一批消息:

https://firebase.google.com/docs/cloud-messaging/send-message#send-a-batch-of-messages

// Create a list containing up to 500 messages.
const messages = [];
messages.push({
  notification: {title: 'Price drop', body: '5% off all electronics'},
  token: registrationToken,
});
messages.push({
  notification: {title: 'Price drop', body: '2% off all books'},
  topic: 'readers-club',
});

return admin.messaging().sendAll(messages)
  .then((response) => {
    console.log(response.successCount + ' messages were sent successfully');
  });

【讨论】:

    【解决方案2】:

    您需要汇总您正在执行的所有异步操作。在这里,您正在对消息回执执行forEach,但随后您将返回一个承诺。尝试类似:

    var promises = [];
    receipts.forEach(function (receipt) {
        //Send push to receipt
        promises.push(admin.database().ref('/users/' + receipt.key).once('value').then(usnap => {
            /* ... */
        }))
    })
    
    return Promise.all(promises);
    

    这会将所有未完成的通知汇总到一个 Promise.all 调用中,该调用将等待它们全部完成。

    【讨论】:

    • 伟大的@Michel Bleigh 你解决了我的问题,我获得了知识。谢谢
    猜你喜欢
    • 2017-11-16
    • 2017-10-08
    • 2018-11-04
    • 2017-10-29
    • 2020-05-26
    • 1970-01-01
    • 2014-11-20
    • 2018-01-15
    • 2020-05-11
    相关资源
    最近更新 更多