54

enter image description here

//body its like this

{
    "to":
    "/topics/NEWS"
    ,
    "data":{
        "extra_information": "This is some extra information"
    },

//notification that i need to give

"notification":{
            "title": "ChitChat Group",
            "text": "You may have new messages",
            "click_action":"ChatActivity"
        }
    }
AL.
  • 36,815
  • 10
  • 142
  • 281
HemalHerath
  • 1,006
  • 2
  • 18
  • 38

4 Answers4

141

The 401 error pertains that your Authorization Key is invalid or incorrect.

When using Postman, add a key= prefix for the value of Authorization, like so:

key=AAA...

See below for a tutorial on Sending Downstream FCM Messages using Postman.

Also, for your notification message payload, text isn't one of the valid parameters, I think you were looking for message instead.



Sending Downstream Messages using Postman

To do this in Postman, you simply have to set the following:

  1. Set request type to POST
  2. In the Headers, set the following:
  3. Set the payload parameters in the Body (*in this example, we used the raw option, see screenshot (2)*)
  4. Send the request to https://fcm.googleapis.com/fcm/send

Screenshots:

(1) enter image description here

Note: Always keep your Server Key a secret. Only a portion of my key is visible here so it should be fine.

(2) enter image description here

(3) enter image description here

Notice that the request was a success with the message_id in the response.

Graham
  • 7,431
  • 18
  • 59
  • 84
AL.
  • 36,815
  • 10
  • 142
  • 281
  • 1
    Thank you @AL. you saved my tons of recherche to read! It's a pitty that `Postman` isn't a resource in the documentation of `Firebase`... I'm working under `windows` and there is no `cURL` built in! – webprogrammer Feb 12 '18 at 15:16
  • 1
    @webprogrammer You're welcome. :) Actually, the instructions I made used to be in the StackOverflow documentation, but the service closed a few months ago. I guess there are a lot of options available and Firebase doesn't want to seem like they're being biased with a service. Cheers! – AL. Feb 13 '18 at 11:35
  • After sending notification from firebase console, if the app is not in foreground it won't receive in `onMessageReceived` method of `MyFirebaseMessagingService.java` class. Tapping that notification, It will take us to launcher activity. From there we have to call this `https://fcm.googleapis.com/fcm/send` url using retrofit, passing what data we have received in launcher activity. And after that response will hit the `onMessageReceived` method of `MyFirebaseMessagingService.java` class. Is it correct?@AL. – Maulik Dodia Jul 04 '18 at 14:45
  • @AL. get message_id but not created in fcm notification history and also not received notification – Bhuvaneshwaran Vellingiri Sep 21 '18 at 11:15
  • Hi, Can you please look at my question regarding firebase https://stackoverflow.com/questions/52523615/firebase-trace-showing-wrong-median-time-latency – Sudhanshu Gaur Sep 27 '18 at 17:53
  • 3
    Hi! I also didn't receive the message but found out that changing the body message on screenshot 2 the push arrived to the phone `{ "to" : "", "data" : { "body":"Message...", "title":"title..", "score":"880", "time":"02:45" } }` – rotoxl Nov 12 '18 at 11:57
  • how to send topic notifications from postman? – tinto mathew Dec 30 '20 at 04:18
22

Wrong:

Authorization:AIzaSyDDk77PRpvfhh......

Correct:

Authorization:key=AIzaSyDDk77PRpvfhh......

Full example:

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..."
}
Mike Yang
  • 2,581
  • 3
  • 24
  • 27
  • 2
    Yes! My time was completely wasted by Google trash docs https://firebase.google.com/docs/cloud-messaging/concept-options with wrong "token" insead of correct "to"... – isabsent Sep 06 '19 at 09:16
6

While the answers above are still correct, you may choose to use HTTP v1. This requires Bearer instead of key= and uses an Oauth2 access token instead of a server key string. To view HTTP v1 specifications, please refer to the link below:

https://firebase.google.com/docs/cloud-messaging/migrate-v1

Ben Grady
  • 61
  • 1
  • 3
1

I was also getting same error in PHP , solved with below header :

$header = array("authorization: key=" .  $this->apiKey . "","content-type: application/json");
Saurabh Mistry
  • 12,833
  • 5
  • 50
  • 71