Firebase云功能始终超时

Gna*_*mmo 11 javascript node.js firebase google-cloud-functions firebase-cloud-messaging

我正在探索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);

    });

});
Run Code Online (Sandbox Code Playgroud)

加上似乎从未更新过badgeNumber,是否与超时问题有关?

Mic*_*igh 23

HTTP触发的云功能就像Express应用程序一样 - 您有一个响应对象(res),您需要在请求完成时发送一些内容.在这种情况下,看起来你可以做类似的事情:

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