【问题标题】:Firebase cloud function always timeoutFirebase 云功能总是超时
【发布时间】:2017-07-31 00:45:38
【问题描述】:

我正在探索 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 好像一直没更新,是不是跟超时问题有关?

【问题讨论】:

  • 对于可能遇到此线程的其他人,还请记住,向第三方发出的 HTTPS 请求需要您已支付 Firebase 帐户;您不能在免费帐户上向 Google 外部发送 HTTP 请求以防止滥用。
  • 哦,我明白了,您从哪里找到这些信息的?数据库触发器呢?
  • 在 Firebase 定价页面上,脚注 3 显示“Spark 计划仅允许向 Google 拥有的服务发出出站网络请求。在 Blaze 计划中,Cloud Functions 提供永久免费层。” firebase.google.com/pricing

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


【解决方案1】:

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');
});

【讨论】:

【解决方案2】:

@Michael Bleigh 的回答非常适合这个问题,让我为未来的用户添加更多内容。

根据 firebase 文档:-

使用这些推荐的方法来管理您的生命周期 功能:

  • 解析执行异步处理的函数(也称为 “后台函数”)通过返回 JavaScript promise

  • 使用res.redirect()res.send()res.end() 终止HTTP 函数。 (本题的情况。)

  • 使用return; 语句终止同步函数。

注意 管理函数的生命周期以确保其正确解析非常重要。通过正确终止函数,您可以避免函数运行时间过长或无限循环产生过多费用。此外,您可以确保运行您的函数的 Cloud Functions 实例在您的函数成功达到其终止条件或状态之前不会关闭。

  • 您需要付费计划(Blaze,即用即付)才能访问外部 API。

如果未配置计费帐户,您可能会在 firebase 函数日志中看到以下警告。

未配置结算帐户。外部网络无法访问并且 名额受到严格限制。配置结算帐户以删除这些 限制

查看this link了解更多信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-17
    • 2020-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-13
    • 2017-08-26
    • 1970-01-01
    相关资源
    最近更新 更多