Firebase云端function总是超时
我正在探索Firebase云计算function,并试图通过http请求发送通知。
问题是,即使我设法发送通知,请求总是超时。
这是我的脚本
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触发的云function就像Express应用程序一样工作 – 您有一个响应对象( res
),您可以在请求完成时使用它来发送内容。 在这种情况下,它看起来像你可以做这样的事情:
return Promise.all([ /* ... */ ]).then(() => { res.status(200).send('ok'); }).catch(err => { console.log(err.stack); res.status(500).send('error'); });
- Firebase Angular 4基于节点环境进行初始化
- 无法执行Firebase部署 – 错误:指定的Firebase无效
- 迁移到新控制台后,Firebase部署被破坏 – “错误:需要身份validation”。
- 从另一个云function调用云function
- 较新版本的Firebase Node.js SDK初始化不起作用 – 旧版本不能
- 当我的Node.js应用程序处于脱机状态时,是否应将Firebase数据库设置为脱机?
- 使用Firebase进行NodeJS身份validation
- 从嵌套字典中创build一个数组
- Firebase的云端function – 未configuration帐单帐户