我想尝试使用谷歌的 FCM
消息服务发送消息,正如文档所述,http 请求应该是这样的:
https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA
{ "data": {
"score": "5x1",
"time": "15:10"
},
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
}
我的问题是我不知道 Authorization
的值应该是什么,当我删除此 header 并发出请求时,我收到错误 401:Unauthorized
。我认为这一定是某种 api key 或其他东西,但我在我的项目中找不到它。谁能帮帮我?
PS:出于测试目的,我目前正在使用 this site向我的设备发送消息
最佳答案
根据About Firebase Cloud Messaging Server文档:
Authentication
To send a message, the app server issues a POST request. For example:
https://fcm.googleapis.com/fcm/send
A message request consists of two parts: the HTTP header and the HTTP body.
The HTTP header must contain the following headers:
- Authorization: key=YOUR_SERVER_KEY
确保这是 server 键,其值在您的 Firebase 项目控制台中的 Project Settings> Cloud Messaging 下可用。 FCM 拒绝客户端 key 或任何其他 key 。
Content-Type:
application/json
for JSON;application/x-www-form-urlencoded;charset=UTF-8
for plain text.If Content-Type is omitted, the format is assumed to be plain text.
更新 (2016/11/16)
也许您应该考虑使用 FCM token 而不是服务器 key 作为以下屏幕截图中的注释
更新 (2016/12/08)
Firebase 已将服务器 key 升级到新版本,并建议您升级到最新版本。请看下面的截图:
关于android - Google 的 Firebase 下游消息的 http post 请求的 `Authorization` 部分是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37673205/