我尝试使用Postman测试FCM,但即使FCM令牌存在,我也总是会收到以下错误。我在Cloud Messaging选项卡中获得了令牌:Firebase Cloud Messaging令牌。
<HTML>
<HEAD>
<TITLE>The request was missing an Authentification Key (FCM Token). Please, refer to section "Authentification" of the FCM documentation, at https://firebase.google.com/docs/cloud-messaging/server.</TITLE>
</HEAD>
这是我发送的内容。
POST /fcm/send HTTP/1.1
Host: fcm.googleapis.com
Cache-Control: no-cache
Postman-Token: 9109eb13-245f-0786-21a5-6207f5426b44
Content-Type:application/json
Authorization:key=AAAAfnYrKvU:APA91bFwgeM3zuFId6UDzvIHk9qZ3lKHnX-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
{ "data": { "message": "This is a Firebase Cloud Messaging Topic Message!", } }:
答案 0 :(得分:17)
花了几个小时后,我发现在邮递员中你必须把以下内容放在标题中。
Key: Content-Type
Value: application/json
Key: Authorization
Value: key=AAAAfnYrKvU:APA91bFwgeM3zuFId6UDzvIHk9qZ3lKHnX-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
(Firebase Cloud Messaging token)
然后单击Body并选择Raw,在这里添加json。
{
"data": {
"title": "new messages",
"score": "5x1",
"time": "15:10"
},
"to": "/topics/alldevices"
}
我还发现你不能通过删除“to”来发送到所有设备:你必须让你的应用订阅一个主题。在我的情况下,我让我的应用程序订阅了“alldevices”。
现在我可以发送“to”:“/ topics / alldevices”,所有应用都会收到通知。
答案 1 :(得分:10)
像我这样的工作代码 -
POST: - https://fcm.googleapis.com/fcm/send
表头 -
Content-Type: application/json
Authorization:key=AAAATIOk_eI:APA91bHR-NRuK-cVTc0fsdQ-N4SOAzocN7ngomFzcV7GkeCCHb6PmCFl_7MXTEPbdw-r0MTU9UmSbyxaSxxxxxxxxx.....
车身 -
{
"registration_ids": ["fBclzMXz1UQ:APA91bE268ddn8DNB95LcU2XyhjjOXE-8PJ1nZ8y0yf1-4UuUX0fFNuae9Acj5BLYZwJq72tnNUjcUax9ZvRxxxxxxxxxxxxxxxxx...."],
"notification": {
"title": "Hello",
"body": "This is test message."
}
}
答案 2 :(得分:1)
工作代码...
make sure You subscribe to topic ="/topics/alldevices" in your android/iOS code.
POST:-https://fcm.googleapis.com/fcm/send
标题-
Content-Type: application/json
Authorization:key=AAAAPfs2N44:APA91bFcDkUfTjbFQvrttpedPcZINcjNkofU_x35xxxxxxxxx.....
身体-
"notification":{
"title":"TITLE",
"body":"BODY",
"sound":"default",
"click_action":"FCM_PLUGIN_ACTIVITY",
"icon":"fcm_push_icon"
},
"data":{
"landing_page":"second",
"price":"$3,000.00"
},
"to":"/topics/alldevices",
"priority":"high",
"restricted_package_name":""
}
答案 3 :(得分:0)
这是一个使用令牌向设备发送通知的邮递员POST请求示例。
Type: POST
Url: https://fcm.googleapis.com/fcm/send
Headers
key: Content-Type,
value: application/json
key: Authorization,
value: key="This is the key in your FCM project console->Settings->Cloud Messaging->Server Key""
body: "mode": "raw"
{
"to": "Token/s received to mobile end",
"notification" : {
"body" : "message",
"content_available" : true,
"priority" : "high",
"title" : "title of the notification"
}
}
答案 4 :(得分:0)
这是邮递员为单个移动设备发送推送通知的示例请求
post - https://fcm.googleapis.com/fcm/send
content-type: application/json
authorization: key="<server key>"
JSON Body
{
"notification":{
"title":"Ionic 4 Notification",
"body":"This notification sent from POSTMAN using Firebase HTTP protocol",
"sound":"default",
"click_action":"FCM_PLUGIN_ACTIVITY",
"icon":"fcm_push_icon"
},
"data":{
"title":"Ionic 4 Notification",
"body":"Ionic test 11",
"sound":"default",
"click_action":"FCM_PLUGIN_ACTIVITY",
"icon":"fcm_push_icon"
},
"to":"<put token id here>",
"priority":"high",
"restricted_package_name":""
}
答案 5 :(得分:0)
documented in the GAS manual的答案使我意识到我的服务器密钥不正确,我以某种方式使用了错误的密钥。我从Firebase控制台获得了serverKey:
它在request.setValue("key=\(serverKey)", forHTTPHeaderField: "Authorization")
下面的代码中使用:
var apsDict = [String: Any]()
// add values
guard let url = URL(string: "https://fcm.googleapis.com/fcm/send") else { return }
let serverKey = "AAAA ..."
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.httpBody = try? JSONSerialization.data(withJSONObject: apsDict, options: [])
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
// *** serverKey is used here ***
request.setValue("key=\(serverKey)", forHTTPHeaderField: "Authorization")
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
do {
if let jsonData = data {
print("jsonData: \(String(data: jsonData, encoding: .utf8)!)")
if let jsonDataDict = try JSONSerialization.jsonObject(with: jsonData, options: .allowFragments) as? [String: AnyObject] {
print("jsonData Success Received data:\n\(jsonDataDict))")
}
}
} catch let err as NSError {
print("jsonData Error: \(err.debugDescription)")
}
}
task.resume()