12

I'm trying to test FCM using Postman, but I always get the following error even the FCM token is there. I got the token in the Cloud Messaging tab: Firebase Cloud Messaging token.

<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>

Here is what I send.

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!",    } }:
Kim HJ
  • 1,183
  • 2
  • 11
  • 37
  • For some more: https://stackoverflow.com/questions/45309674/fcm-with-postman-the-request-was-missing-an-authentication-key-fcm-token/45310143 – Srikrushna Jun 19 '19 at 14:34

6 Answers6

20

After spending some hours I found that in Postman you have to put the following in the Headers.

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

Then click Body and select Raw, here you add the json.

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

I also found that you cannot send to all devices by eliminating the "to": You will have to have your app subscribe to a topic. In my case I made my app subscribed to "alldevices".

Now I can send "to":"/topics/alldevices" and all apps will receive the notification.

Ruslan Osmanov
  • 20,486
  • 7
  • 46
  • 60
Kim HJ
  • 1,183
  • 2
  • 11
  • 37
  • Was about to add in a comment earlier, but got lost in time. Steps you did is the same as the sample I provided in this [documentation](http://stackoverflow.com/documentation/google-cloud-messaging/5811/getting-started-with-google-cloud-messaging/20474/send-downstream-messages-from-the-cloud#t=201611290600158576573). Good job. – AL. Nov 29 '16 at 06:02
  • You can also send to individual devices by providing an [InstanceID](http://stackoverflow.com/questions/37671380/what-is-fcm-token-in-firebase/37671576#37671576) (token) in the "to" field. – Nathan Nov 29 '16 at 13:06
  • 1
    Yes, answer is correct. I didn't notice that in my case was the "key=" before token itself. – brunoramonalmeida May 07 '17 at 19:31
11

working code for me like this-

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."
    }
}
Peter Todd
  • 8,561
  • 3
  • 32
  • 38
GauravInno
  • 317
  • 1
  • 2
  • 14
1

This is a sample postman POST request to send notifications to devices by using tokens.

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"
     }
    }
ireshika piyumalie
  • 2,226
  • 22
  • 22
1

working code...

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":"$3,000.00"
},
"to":"/topics/alldevices",
"priority":"high",
"restricted_package_name":""
}
bunny
  • 1,316
  • 2
  • 13
  • 22
0

This is the sample postman request to send push notification for single mobile device

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":""
}
Anand Acharya
  • 89
  • 1
  • 3
0

@GauravInno's answer made me realize my server key was incorrect, I somehow was using the wrong key. I got the serverKey from Firebase console:

enter image description here

It's used in the code below 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()
Lance Samaria
  • 17,576
  • 18
  • 108
  • 256