I\'m trying to use Postman to send a single Push Notification using Firebase
Cloud Messaging service.
This is a working cURL
command for th
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:
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:
Now click on Body
than select Row
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.
Good luck!
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"
}
}
}
}
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"
}
}
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:
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.
Have you check this : Test FCM Notification with POSTMAN!
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.
What exactly is Server Key? I used (on a pitcure above) server key, and legacy server key but neither of them work.
Does anybody have clue what is wrong? For token -> I used FCM token I got from firebase messaging like this:
firestoreMessaging.getToken()