64

I'm trying to use Postman to send a single Push Notification using Firebase Cloud Messaging service.

This is a working cURL command for the same purposal, on which I'm using as a reference.

curl -X POST --header "Authorization: key=<API_ACCESS_KEY>" --Header "Content-Type: application/json" https://fcm.googleapis.com/fcm/send -d "{\"to\":\"<YOUR_DEVICE_ID_TOKEN>\",\"notification\":{\"body\":\"Firebase\"} \"priority":\"10"}"

What I have done so far..

1 - Set the Headers appropriately

enter image description here

2- At Body , I'm using raw

{
    "to" : "<YOUR_DEVICE_ID_TOKEN>"
    , 

    "notification": {
    "body": "Firebase Cloud Message"
  }

  }

When executing, I'm getting back 401 - Unauthorized.

What's missing to correctly send the push notification?

4

14 Answers 14

62

The correct way to set up Authorization key at Header is

key=<API_ACCESS_KEY>

and not only

<API_ACCESS_KEY>

Silly mistake, but since this could be useful for someone for testing Firebase Messaging with Postman I'm leaving the question opened.

1
  • I see, you already have --header "Authorization: key=<API_ACCESS_KEY> in curl and wrong header in Postman.
    – CoolMind
    Jun 25, 2019 at 15:40
57

Open Postman, click on Enter request URL textbox, enter firebase url

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

Than change request type to POST. See below screenshot:

enter image description here

Now to click on Header and add two params Content-Type and Authorization.

Content-Type= application/json
Authorization=AIzaSyAEb3NhsfGw1ih5mn8cmrGUAXMp86waQ //FCM SERVER KEY

See below screenshot:

enter image description here

Now click on Body than select Raw and add value as object like below

{
  "to": "eB5papU2Xdc:APA91bFFvc3dXru1fN5JY8U19oHIpfGhPUx7Ll7v9vJYTsIGZ15mDwB2Wpep3flLK85IUqqs2WqJwjYHSDYX28oJ1wTP0R2TDc2ba_uVjUauDcp3pCNKr_0KlghOnS",
  "notification": {
    "body": "New announcement assigned",
    "OrganizationId": "2",
    "content_available": true,
    "priority": "high",
    "subtitle": "Elementary School",
    "title": "hello"
  },
  "data": {
    "priority": "high",
    "sound": "app_sound.wav",
    "content_available": true,
    "bodyText": "New Announcement assigned",
    "organization": "Elementary school"
  }
}

Now your Postman would look like this. Click Send button at top right.

enter image description here

Good luck!

5
  • Thanks, but currently it shows an error in HTML: "<TITLE>Invalid (legacy) Server-key delivered or Sender is not authorized to perform request.</TITLE>".
    – CoolMind
    Jun 25, 2019 at 9:55
  • This works if you use a server key or legacy server key from Cloud Messaging settings (starting from "AAAA" or "Alza").
    – CoolMind
    Jun 25, 2019 at 15:33
  • This sends to the given token only. How can I send it to all users from postman?
    – Neo
    Dec 5, 2020 at 7:34
  • This works like a charm and sends notifications instantly. A thing that should be changed is that Title to title !
    – clauub
    Apr 15, 2021 at 8:55
  • This worked. serverKey I had to copy deprecated/legacy key. Where do I get the updated key? P.S. Thanks for the detailed answer with copyable samples.
    – anotherDev
    Nov 12 at 7:59
36

Posting FCM through POSTMAN

Body - to is token id (should be generated through instance token) write body in raw binary application/json body tye

{
   "to": "cpa8cZPjq-w:APA91bF122f1Rnhu9v47bL
   YMajaNTHAIU5SzItDwTy9o2MCIveG0PlK78VPvp3d
   CqjwnUKZ4
   ngi1trSyM3_aXttW62iknFfbPGtjRLhZr6wq-3qFdboz8gzdOGPz**********",

   "notification": {

    "body": "Hello",
    "title": "This is test message."
   }
   }

header: should have authorization :server key

Content type : application/json headerimage after posting here the success message: success message image

9
  • 1
    how to get to token? is there an API?
    – Asymptote
    Jul 24, 2017 at 13:54
  • Read firebase android developer documentation Jul 24, 2017 at 16:58
  • 2
    am getting sucess=1 but am not getting a notification on mobile
    – phpdroid
    Aug 23, 2017 at 8:09
  • Hi phpdroid you need to check complete body, message is passing to your mobile but it is not able to pick the body Sep 9, 2017 at 4:52
  • how to send to all users, who installed app, instead of send to topic?
    – DevOma
    Dec 14, 2018 at 11:41
25

For the new FCM HTTP v1 API, the method of testing push notifications through Postman has changed and the existing solutions only addressed the legacy method of testing push notifications: https://firebase.google.com/docs/cloud-messaging/send-message

To test on Postman with FCM HTTP v1 API, you will need to first fetch a short-lived Oauth 2 token. You can generate one as per the information on the Firebase site here: https://firebase.google.com/docs/cloud-messaging/auth-server

I generated mine using Python using the code below - remember to pip install the package so that you can import the package into your code successfully:

from oauth2client.service_account import ServiceAccountCredentials

def _get_access_token():
  """Retrieve a valid access token that can be used to authorize requests.

  :return: Access token.
  """
  credentials = ServiceAccountCredentials.from_json_keyfile_name(
      'service-account.json', FCM_SCOPE)
  access_token_info = credentials.get_access_token()
  return access_token_info.access_token

Once you have the token - you can insert it into your Postman under authorisation:

enter image description here

IMPORTANT:

In my case, I had struggled a bit to get this to work as I was trying to test a scheduled task that was going to send push notification every few minutes to FCM. I had printed the token to my logs and then taken that token to my Postman for testing, not realising that it already has been used in the scheduled calls to FCM.

In that case, because it has already been used, the token will no longer be valid and my Postman tests were all failing. In this case, you will need to generate fresh tokens for your Postman tests.

2
  • I am getting this response but not the notification to the android app. Any idea?
    – Shridhar
    Jan 10, 2021 at 15:05
  • @Shridhar I have the same problem, did you manage to find a solution ?
    – rgantla
    Nov 11, 2022 at 21:55
13

POST

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

Header

Content-Type : application/json 
Authorization : key=your_server_key

Body

{ 
    "to" : "your_token",
    "priority": "high",
    "notification": {
        "title": "Title",
        "body" : "First Notification",
        "text": "Text"
    }
}
2
  • 3
    This method allows it to send to the specified token. How can we send to all the users?
    – Neo
    Dec 5, 2020 at 7:36
  • "to": "/topics/All", and subscribe that topic Nov 7, 2022 at 10:19
11

Look at below screenshot how Authorization key is set

Authorization : **key=**abcdefghijklmnopr2qrst253uv124wxyz_9shg

enter image description here

6

Have you check this : Test FCM Notification with POSTMAN!

Send Data Message using HTTP protocol with POSTMAN

  • You have to copy Legecy Server Key from Firebase Console > Project Settings > Cloud Messaging

Note: Firebase has upgraded our server keys to a new version. You may continue to use your Legacy server key, but it is recommended that you upgrade to the newest version.

  • Select POST. Enter request URL as https://fcm.googleapis.com/fcm/send

  • Add Headers Authorization: key=<legacy_server_key> OR Authorization: key=<server_key> and Content-Type: application/json.

  • Now Select Body > raw > JSON (application/json) and add following code:

    {
         "to" : "YOUR_FCM_TOKEN_WILL_BE_HERE",
         "collapse_key" : "type_a",
         "notification" : {
             "body" : "Body of Your Notification",
             "title": "Title of Your Notification"
         },
         "data" : {
             "body" : "Body of Your Notification in Data",
             "title": "Title of Your Notification in Title",
             "key_1" : "Value for key_1",
             "key_2" : "Value for key_2"
         }
    }

I have created POSTMAN Collection for you, Run in Postman directly.

Hope it will helps you.

3
  • Thanks! At least, it shows a more suitable error, than "Invalid (legacy) Server-key...".
    – CoolMind
    Jun 25, 2019 at 10:01
  • This works if you use a server key or legacy server key from Cloud Messaging settings (starting from "AAAA" or "Alza").
    – CoolMind
    Jun 25, 2019 at 15:33
  • What's the difference between notification and data object? Why do you use both?
    – clauub
    Apr 15, 2021 at 8:57
4
POST https://fcm.googleapis.com/v1/projects/projectID/messages:send

headers:

Authorization:Bearer ya29.c.ElrXBjdxUzOI................o7_Y2shX2Je4tEe0

body:

  {
      "message": {
        "token": "dlqj05MCnNI:APA91bGhCX6NhT.................QDpKhGheHyhhJ6T",
        "android": {
          "notification": {
            "title": "New Content!",
            "body": "A new video has been uploaded.",
            "icon": "your_icon"
          }
        }
      }
    }
2
  • 7
    where you got this Bearer token? If I take the server key, FCM throws 401 Mar 23, 2020 at 13:18
  • This token is generated by the Firebase Admin SDK. Feb 16, 2022 at 19:27
4

Steps to send FCM throw Postman:

  1. URL to send: https://fcm.googleapis.com/v1/projects/projectid-34543/messages:send
  2. Request Type: POST
  3. Headers: Content-Type -> application/json & Authorization -> Bearer
  4. In the body section enter APS payload with the right device token.
  5. Click send.

More information here: https://stackoverflow.com/a/62670409/1151916

3

İf you want to send the notification to more than one device, you should use "registration_ids" instead of "to" on the body;

 { 
    "registration_ids":[
"somethingTokens7234134143",
"somethingTokens3452343185"
], 

     "notification" : {
     "body" : "New announcement assigned",
     "content_available" : true,
     "priority" : "high",
     "subtitle":"Elementary School",
     "Title":"hello"
     },
     "data" : {
     "priority" : "high",
     "sound":"app_sound.wav",
     "content_available" : true,
     "bodyText" : "New Announcement assigned",
     "organization" :"Elementary school"
    }
    }
2

In my case, Cloud Messaging API (Legacy) was disabled from the Firebase console. Check if that section is enabled in your Firebase console enter image description here

0

Be sure to include the content-type : application/json header

it fails with a misleading error if you don't include the content-type header saying: Error=MissingRegistration

0

Here is Postman Curl's request for sending Push notifications -

curl --location 'https://fcm.googleapis.com/fcm/send' \
--header 'Authorization: key={Server Key From Firebase}' \
--header 'Content-Type: application/json' \
--data '{
"to" : "{Device FCM Token}",
"notification" : {
   "body" : "Body of Your Notification",
   "title": "Postman CURL"
}
}'

Copy Server Key From Firebase -

enter image description here

1
  • 1
    Do not use the legacy API. It will be deprecated in June 2024.
    – Jurakin
    Nov 18 at 16:42
-1

enter image description here

What exactly is Server Key? I used (on a pitcure above) server key, and legacy server key but neither of them work.

enter image description here

Does anybody have clue what is wrong? For token -> I used FCM token I got from firebase messaging like this:

firestoreMessaging.getToken()
3
  • I think, you have right header, but maybe a configuration of Firebase project is not done. Have you added an Android or iOS application?
    – CoolMind
    Jun 25, 2019 at 10:04
  • Also you should check google-services.json in your Android application. You need to download it from Firebase console and paste into Android application, then run on device.
    – CoolMind
    Jun 25, 2019 at 15:36
  • This answer is your question: stackoverflow.com/questions/56723045/….
    – CoolMind
    Jun 25, 2019 at 15:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.