【问题标题】:FCM token missing缺少 FCM 令牌
【发布时间】:2017-04-12 22:10:57
【问题描述】:

我正在尝试使用 Postman 测试 FCM,但即使有 FCM 令牌,我也总是收到以下错误。我在 Cloud Messaging 选项卡中获得了令牌:Firebase Cloud Messaging 令牌。

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

【问题讨论】:

标签: firebase-cloud-messaging postman


【解决方案1】:

花了几个小时后,我发现在 Postman 中,您必须将以下内容放在标题中。

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”来发送到所有设备:你必须让你的应用订阅一个主题。就我而言,我让我的应用订阅了“所有设备”。

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

【讨论】:

  • 之前本来打算加个评论的,但是没赶上。您所做的步骤与我在此documentation 中提供的示例相同。干得好。
  • 您还可以通过在“收件人”字段中提供InstanceID(令牌)来发送到各个设备。
  • 是的,答案是正确的。我没有注意到在我的情况下是令牌本身之前的“key=”。
【解决方案2】:

像这样为我工作的代码-

发布:- 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."
    }
}

【讨论】:

  • 'Authorization' 是标头名称,'key=AAAATI...' 是标头值。
  • 抱歉耽搁了。是的,'Authorization' 是标头名称,'key=AAA....' 标头值 @arberg
【解决方案3】:

这是使用令牌向设备发送通知的示例邮递员 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】:

    工作代码...

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

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

    【讨论】:

      【解决方案5】:

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

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

      【讨论】:

        【解决方案6】:

        @GauravInno's 的回答让我意识到我的服务器密钥不正确,我不知何故使用了错误的密钥。我从 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()
        

        【讨论】:

          猜你喜欢
          • 2017-04-05
          • 1970-01-01
          • 2017-12-31
          • 1970-01-01
          • 2020-09-24
          • 2017-01-08
          • 2019-06-01
          • 2015-05-16
          • 2011-04-09
          相关资源
          最近更新 更多