Firebase的云功能将推送发送给多个用户

时间:2017-09-13 16:20:54

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

我尝试在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脚本和云功能的新手。 我需要做什么才能通知所有用户?

2 个答案:

答案 0 :(得分:0)

您需要聚合所有正在执行的异步操作。在这里你正在对邮件收据进行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来电,这将等到它们全部完成。

答案 1 :(得分:0)

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

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');
  });