Testing FCM Push Notification

Testing FCM Push Notification through Postman/Terminal — Part 1

Tutorial to test Firebase Cloud Messaging (FCM) Push Notification through Postman or Terminal (curl command)

Apoorv Garg
4 min readAug 27, 2020
Firebase Cloud Messaging (FCM) + Postman / Terminal → Send Push Notification

While implementation of Firebase Cloud Messaging, there is a need to test the functionality during the development /QA phase. So, developers need a way to unit test the functionalities covering all scenarios, which involves sending push notification multiple times.

Before getting in to the details, let’s get into some basic details about FCM notifications which will help in understanding the testing methods.

Key Points as per FCM documentation:

  • Notification Messages :
    Notification messages are handled by the FCM SDK automatically and displays the message to end-user devices on behalf of the client app. Notification messages have a predefined set of user-visible keys and an optional data payload of custom key-value pairs.
  • Data messages :
    Data messages are handled by the client app and is responsible for processing the same. Data messages have only custom key-value pairs with no reserved key names.
  • Distribute messages to your client app in any of 3 ways — to single devices, to groups of devices, or to devices subscribed to topics
  • Maximum payload for both message types is 4KB, except when sending messages from the Firebase console, which enforces a 1024 character limit.

Methods to test FCM Push Notifications:

  1. Notifications composer
    Limitation: Can only test Notification Messages with this approach.
  2. Legacy FCM HTTP Protocol + Postman / Terminal
    As per documentation, any apps that use device group messaging must continue to use the legacy API for the management of device groups (creating, updating, etc.).
  3. FCM HTTP v1 API + Postman / Terminal
    As per documentation, this is the most up-to-date and most flexible approach for sending messages to multiple platforms.
    Limitation : The HTTP v1 can send messages to device groups, but does not support management.

In the following article, we will only cover Notifications composer and Legacy FCM HTTP Protocol. Covered FCM HTTP v1 API in my subsequent article/post here.

Notifications composer

  • Go to Firebase Console → Select Project → Cloud Messaging → Send your first message
  • Add Notification title and Notification text
  • Click Send test message
  • Add FCM registration token
  • Click Test
Notification composer
Send test message to a registered token (device)

Legacy FCM HTTP Protocol + Postman/Terminal

Send message through Postman or Terminal (curl command)

Copy <Server Key> from Firebase Console → Project Settings → Cloud Messaging

Copy <Server Key>

Note : Starting from March 2020, FCM stopped creating legacy server keys. Existing legacy server keys will continue to work, but firebase recommend that you instead use the newer version of key

Add the following details in the Postman :-

Endpoint : https://fcm.googleapis.com/fcm/send
Method : POST
Headers :-
-Authorization : key = <server_key>
-Content-Type : application/json

POSTMAN — Add Endpoint, Headers and Method type

☛ Send Notification message (Generic)

  • Using Postman : Add the following code in Body → Raw → JSON & click Send
{
"to" : "FCM_TOKEN_OR_TOPIC_WILL_BE_HERE",
"notification" : {
"body" : "Body of Your Notification",
"title": "Title of Your Notification"
}
}
Sending Notification Message (Generic) using POSTMAN
  • Using Terminal :
curl --location --request POST 'https://fcm.googleapis.com/fcm/send' \
--header 'Authorization: key=< Server Key >' \
--header 'Content-Type: application/json' \
--data-raw '{
"to" : "FCM_TOKEN_OR_TOPIC_WILL_BE_HERE",
"notification" : {
"body" : "Body of Your Notification",
"title": "Title of Your Notification"
}
}'

☛ Send Data message (Custom) :

  • Using Postman : Add the following code in Body → Raw → JSON & click Send
{
"to" : "FCM_TOKEN_OR_TOPIC_WILL_BE_HERE",
"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"
}
}
Sending Data Message (Generic) using POSTMAN
  • Using Terminal :
curl --location --request POST 'https://fcm.googleapis.com/fcm/send' \
--header 'Authorization: key=< Server Key >' \
--header 'Content-Type: application/json' \
--data-raw '{
"to" : "FCM_TOKEN_OR_TOPIC_WILL_BE_HERE",
"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"
}
}'

Explanation of Body payload:

Body:-
-to : The value can be a device’s registration token, a device group’s notification key, or a single topic (prefixed with /topics/).
-notification : This parameter specifies the predefined, user-visible key-value pairs of the notification payload
-data : This parameter specifies the custom key-value pairs of the message’s payload

For other parameters refer Firebase link

Finally, we successfully send Notification messages (Generic) and Data messages (Custom) using POSTMAN and Terminal.

Make sure you give this post 50 claps👏 and follow if you enjoyed this post and want to see more.

Thank you for reading. ❤️

Connect with me : LinkedIn

--

--

Apoorv Garg
Apoorv Garg

Written by Apoorv Garg

Senior Consultant at GlobalLogic | iOS app developer | AWS Certified Solutions Architect - Associate