gpt4 book ai didi

javascript - Firebase 云功能总是超时

转载 作者:IT老高 更新时间:2023-10-28 23:18:44 24 4
gpt4 key购买 nike

我正在探索 firebase 云功能,并尝试使用 http 请求发送通知。

问题是即使我设法发送通知,请求总是超时。

这是我的脚本

/functions/index.js
const functions = require('firebase-functions');
const admin = require('firebase-admin');

admin.initializeApp(functions.config().firebase);

exports.friendRequestNotification = functions.https.onRequest((req, res) => {

const senderId = req.query.senderId;
const recipientId = req.query.recipientId;
const getRecipientPromise = admin.database().ref(`/players/${recipientId}`).once('value');
const getSenderPromise = admin.database().ref(`/players/${senderId}`).once('value');

return Promise.all([getRecipientPromise, getSenderPromise]).then(results => {

const recipient = results[0];
const sender = results[1];

const recipientToken = recipient.child("notificationsInfo/fcmToken").val();
const notificationAuthorization = recipient.child("notificationsInfo/wantsToReceiveNotifications").val();
const recipientBadge = recipient.child("notificationsInfo/badgeNumber").val();
const senderUsername = sender.child("username").val();

const payload = {
notification: {
title: `FriendRequest`,
body: `You have a new friend request from ${senderUsername}!`,
badge: (recipientBadge+1).toString()
}
};

if (notificationAuthorization) {

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

});

}

return admin.database().ref(`/players/${recipientId}/notificationsInfo/badgeNumber`).setValue(recipientBadge+1);

});

});

另外,badgeNumber 好像一直没更新,是不是跟超时问题有关?

最佳答案

HTTP 触发的 Cloud Functions 的工作方式与 Express 应用程序一样——您有一个响应对象 (res),您需要使用该对象在请求完成时发送某些内容。在这种情况下,您似乎可以执行以下操作:

return Promise.all([
/* ... */
]).then(() => {
res.status(200).send('ok');
}).catch(err => {
console.log(err.stack);
res.status(500).send('error');
});

关于javascript - Firebase 云功能总是超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42705398/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com