【问题标题】:Firebase Cloud Messaging Notification not received in web appWeb 应用程序中未收到 Firebase 云消息传递通知
【发布时间】:2020-11-07 07:35:56
【问题描述】:

我正在使用 FCM 进行通知。 FCM 在从 Firebase 数据库创建数据时触发。我收到了第一条消息。之后,就没有收到其他连续的消息了。我在本地环境中运行它。问题是由于以下消息“未配置计费帐户。无法访问外部网络并且配额受到严重限制。配置计费帐户以消除这些限制”或任何其他问题。我是否需要进入接收消息的计费计划。在测试环境中工作,这就是不转向计费计划的原因。如果问题与计费计划无关,有人可以指出代码的任何其他问题。

Firebase 功能日志

6:22:52.133 PM
sendFollowerNotification
Function execution started
6:22:52.133 PM
sendFollowerNotification
Billing account not configured. External network is not accessible and quotas are severely limited. Configure billing account to remove these restrictions
6:22:52.143 PM
sendFollowerNotification
Function execution took 10 ms, finished with status: 'ok'
6:22:52.401 PM
sendFollowerNotification
1 messages were sent successfully

节点js代码

exports.sendFollowerNotification = functions.database.ref('/notification/message/{gId}/{pId}')
        .onCreate(async (change, context) => {
    
          //console.log('Group id:', context.params.gId," Push ID:",context.params.pId, "Change",change);
          
          const notificationData =  change.val();
          var topic = notificationData.topic;
          var title = notificationData.title;
          var body = notificationData.body;
          var registrationTokens = notificationData.tokens;
    
          const message = {
            notification: {
              title: title,
              body: body
            },
            tokens: registrationTokens,
          };
    
          admin.messaging().sendMulticast(message)
          .then((response) => {
            // Response is a message ID string.
            console.log(response.successCount + ' messages were sent successfully');
          })
          .catch((error) => {
            console.log('Error sending message:', error);
          });
          
        });

【问题讨论】:

    标签: firebase push-notification google-cloud-functions firebase-cloud-messaging


    【解决方案1】:

    该消息并不表示错误。这只是一个警告,让您知道如果您的项目不在付款计划中,出站网络将不起作用。 FCM 消息不属于这一类 - 它应该可以工作。

    问题是您的代码没有返回在所有异步工作完成后解决的承诺。现在,它什么也不返回,并且函数在消息发送之前立即终止。请阅读并了解documentation关于此的内容。

    至少,您需要返回承诺链,让 Cloud Functions 知道消息何时发送,并且可以安全地终止。

              return admin.messaging().sendMulticast(message)
              .then((response) => {
                // Response is a message ID string.
                console.log(response.successCount + ' messages were sent successfully');
              })
              .catch((error) => {
                console.log('Error sending message:', error);
              });
    

    注意上面的 return 关键字。

    如果消息仍然没有被发送,那么这里还有一些我们看不到的问题。您可能没有正确处理您的设备令牌。

    【讨论】:

    • 非常感谢,很抱歉延迟回复您。它工作得很好。
    【解决方案2】:

    我想这可能会回答你的问题:Why will I need a billing account to use Node.js 10 or later for Cloud Functions for Firebase?:

    由于计划于 2020 年 8 月 17 日更新其底层架构,Cloud Functions for Firebase 将依赖一些额外的 Google 付费服务:Cloud Build、Container Registry 和 Cloud Storage。这些架构更新将适用于部署到 Node.js 10 运行时的功能。除了现有定价之外,还将对这些服务的使用收费。

    在新架构中,Cloud Build 支持功能部署。您只需为构建函数的运行时容器所需的计算时间付费。

    另一方面,Firebase Clud Messaging 服务本身是免费的:

    Firebase 云消息传递 (FCM) 在您的服务器和设备之间提供可靠且省电的连接,让您可以在 iOS、Android 和网络上免费发送和接收消息和通知。

    鉴于您在 CF 中使用 Node,平台需要您一个结算帐户。

    【讨论】:

    • 这实际上根本不是问题。如果是,那么就会出现实际错误,而不仅仅是警告。
    • 看来账单警告并不是没有收到通知的真正原因。无论如何感谢您的回复。
    猜你喜欢
    • 2020-07-16
    • 2020-06-16
    • 2019-02-06
    • 2020-07-28
    • 2017-12-24
    • 1970-01-01
    • 2017-05-05
    • 2022-09-30
    • 2019-10-08
    相关资源
    最近更新 更多