【问题标题】:Sending FCM push in Node在 Node 中发送 FCM 推送
【发布时间】:2016-12-31 23:11:03
【问题描述】:

我在向 Firebase Cloud Messaging by Node 应用程序发送消息时遇到问题。

 request({
        url: 'https://fcm.googleapis.com/fcm/send',
        method: 'POST',
        headers: {
          'Content-Type' :' application/json',
          'Authorization': 'key=AIzaSyAF9cvEThRo3ZlWCLuSU5k6W9kk0uumkLM',
          'project_id': '83933810320'
        },
        body: JSON.stringify(
          { "data": {'notification': notification, _id: _id, action: action},
            "registration_ids" : registration_ids,
            "content_available": true,
          }
        )
      }, function(error, response, body) {
        if (error) { 
          console.error(error, response, body); 
        }
        else if (response.statusCode >= 400) { 
          console.error('HTTP Error: '+response.statusCode+' - '+response.statusMessage+'\n'+body); 
        }
        else {
          console.log(response);
        }
      });

当我调用上面的代码时,它给我的响应是 401 Unauthorized。我无法理解为什么会收到此错误。我使用了正确的服务器密钥。我使用的策略是否有语法错误或任何错误。

【问题讨论】:

  • 您是否检查过您的服务器 API 密钥是否有效?我今天遇到了同样的问题,我的 API 密钥在 Cloud Messaging 下的 Firebase 网站中有所不同。所以我不得不再次更改 API 密钥。
  • 我更正了我在 firebase 中创建了一个 android 应用程序“将 Firebase 添加到您的 Android 应用程序”中的问题,并且我昨天复制了 google-service.json 中存在的 api_key,它可以工作但今天不行!我通过在“将 Firebase 添加到您的网络应用程序”中的 cklick 解决了这个问题,我复制了 apiKey apiKey 它与 google-service.json 中的 api_key 不同

标签: node.js firebase firebase-cloud-messaging


【解决方案1】:

您需要使用云消息密钥,不同于其他 API 密钥。

要获取密钥,请转到 https://console.firebase.google.com/project/:project-name/settings/cloudmessaging(将 :project_name 替换为您的项目名称)

【讨论】:

    【解决方案2】:

    您可以将 fcm 消息从 Web 应用程序发送到移动设备或主题。

    将 fcm 发送到设备: https://firebase.google.com/docs/cloud-messaging/send-message#send_messages_to_specific_devices

    将 fcm 发送到主题: https://firebase.google.com/docs/cloud-messaging/send-message#http_post_request

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-06
      • 1970-01-01
      • 2020-09-17
      • 1970-01-01
      • 2022-08-22
      • 2018-10-29
      • 1970-01-01
      相关资源
      最近更新 更多