缺少 FCM 令牌

FCM token missing

我正在尝试使用 Postman 测试 FCM,但即使有 FCM 令牌,我也总是收到以下错误。我在“云消息传递”选项卡中获得了令牌:Firebase 云消息传递令牌。

<HTML>
<HEAD>
    <TITLE>The request was missing an Authentification Key (FCM Token). Please, refer to section &quot;Authentification&quot; 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!",    } }:

花了几个小时后,我发现在 Postman 中你必须将以下内容放入 Headers。

Key: Content-Type
Value: application/json
Key: Authorization
Value: key=AAAAfnYrKvU:APA91bFwgeM3zuFId6UDzvIHk9qZ3lKHnX-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
(Firebase Cloud Messaging token)

然后点击Body和select Raw,这里添加json.

    {
        "data": {
            "title": "new messages",
            "score": "5x1",
            "time": "15:10"
        },
        "to": "/topics/alldevices"
    }

我还发现您不能通过消除 "to" 来发送到所有设备:您必须让您的应用程序订阅一个主题。就我而言,我让我的应用程序订阅了 "alldevices".

现在我可以发送 "to":“/topics/alldevices”,所有应用程序都会收到通知。

像这样适合我的工作代码-

POST:- https://fcm.googleapis.com/fcm/send

Header-

 Content-Type: application/json
 Authorization:key=AAAATIOk_eI:APA91bHR-NRuK-cVTc0fsdQ-N4SOAzocN7ngomFzcV7GkeCCHb6PmCFl_7MXTEPbdw-r0MTU9UmSbyxaSxxxxxxxxx.....

Body-

 {
"registration_ids": ["fBclzMXz1UQ:APA91bE268ddn8DNB95LcU2XyhjjOXE-8PJ1nZ8y0yf1-4UuUX0fFNuae9Acj5BLYZwJq72tnNUjcUax9ZvRxxxxxxxxxxxxxxxxx...."],
"notification": {
    "title": "Hello",
    "body": "This is test message."
    }
}

这是一个示例邮递员 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"
     }
    }

工作代码...

make sure You subscribe to topic ="/topics/alldevices" in your android/iOS code.

POST:- https://fcm.googleapis.com/fcm/send

Header-

Content-Type: application/json
Authorization:key=AAAAPfs2N44:APA91bFcDkUfTjbFQvrttpedPcZINcjNkofU_x35xxxxxxxxx.....

Body-

"notification":{
"title":"TITLE",
"body":"BODY",
"sound":"default",
"click_action":"FCM_PLUGIN_ACTIVITY",
"icon":"fcm_push_icon"
},
"data":{
"landing_page":"second",
"price":",000.00"
},
"to":"/topics/alldevices",
"priority":"high",
"restricted_package_name":""
}

这是为单个移动设备发送推送通知的示例邮递员请求

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":""
}

回答让我意识到我的服务器密钥不正确,我不知何故使用了错误的密钥。我从 Firebase 控制台获得了服务器密钥:

在下面的代码中使用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()