Firebase云功能http触发响应功能带循环

时间:2017-12-09 05:00:15

标签: node.js firebase-cloud-messaging google-cloud-functions

我写了一些代码来向我注册到事件的所有用户发送通知,我使用http触发器,当事件我启动时将调用,我无法将通知发送到设备组,因为事件可能不同,即使开始时间是一样的。

我的firebase节点看起来像这样:

registration
    |-- userid1
    |       |-- registration1
    |       |       |-- FirebaseToken
    |       |       |-- EventName
    |       |-- registration2
    |               |-- FirebaseToken
    |               |-- EventName
    |-- userid2

这是我的脚本:

exports.sendNotification = functions.https.onRequest((request, result) => {
    admin.database().ref('/registration').once('value').then(snapshot => {
        snapshot.forEach(childSnapshot => {
            let list = childSnapshot.val()
            for (let key in list) {
                let p = list[key];
                let token = p.FirebaseToken;
                console.log("Device token:", token);

                if (token != null && token != "") {

                    let payload = {
                        notification: {
                            title: `The ${p.EventName}`,
                            body: `The ${p.EventName} will be started soon.`,
                            sound: "default"
                        }
                    };

                    // Set the message as high priority and have it expire after 1 hours.
                    let options = {
                        priority: "high",
                        timeToLive: 60 * 60
                    };

                    admin.messaging().sendToDevice(token, payload, options)
                        .then(response => {
                            console.log("Successfully sent message:", response);
                        })
                        .catch(error => {
                            console.log("Error sending message:", error);
                        });
                }
            };
        });

        result.status(200).send("ok");
    });
});

使用此脚本,我设法将通知发送给用户,但我收到了类似这样的日志Function execution took 1389 ms, finished with status code: 304

是否有任何更正或建议。

0 个答案:

没有答案