Sending Push via Postman using Firebase Messaging

2019-01-11 17:22发布

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?

5条回答
该账号已被封号
2楼-- · 2019-01-11 18:00

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

查看更多
太酷不给撩
3楼-- · 2019-01-11 18:02

Look at below screenshot how Authorization key is set

Authorization : **key=**abcdefghijklmnopr2qrst253uv124wxyz_9shg

enter image description here

查看更多
Juvenile、少年°
4楼-- · 2019-01-11 18:03

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

查看更多
ゆ 、 Hurt°
5楼-- · 2019-01-11 18:04

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.

查看更多
太酷不给撩
6楼-- · 2019-01-11 18:20

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.

查看更多
登录 后发表回答