Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 带循环的函数上的Firebase云函数http触发器响应_Node.js_Firebase Cloud Messaging_Google Cloud Functions - Fatal编程技术网

Node.js 带循环的函数上的Firebase云函数http触发器响应

Node.js 带循环的函数上的Firebase云函数http触发器响应,node.js,firebase-cloud-messaging,google-cloud-functions,Node.js,Firebase Cloud Messaging,Google Cloud Functions,我编写了一些代码向注册到事件的所有用户发送通知,我使用http触发器,在事件启动时将调用该触发器,我无法向设备组发送通知,因为即使启动时间相同,事件也可能不同 我的firebase节点如下所示: registration |-- userid1 | |-- registration1 | | |-- FirebaseToken | | |-- EventName | |-- regis

我编写了一些代码向注册到事件的所有用户发送通知,我使用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");
    });
});
通过这个脚本,我成功地向用户发送了通知,但我得到了一个类似这样的日志,函数执行耗时1389毫秒,状态代码为304


是否有任何更正或建议执行此操作。

有什么问题?
状态代码:304
HI SIRS您是否找到304的解决方案我被困在同一个一次性函数中,在数据库中执行更新值,而其他时间它不工作..发送304@RahulVats我想你需要回报一个承诺,对我来说,在第2行之前添加
return
,并在
return admin.messaging()处发送到设备…
解决问题