Firebase Cloud Messaging important REST API’s

Selvaganesh
5 min readJan 14, 2018

Firebase Cloud Messaging (FCM) is a cross-platform messaging solution that lets you reliably deliver messages at no cost.

For use cases such as instant messaging, a message can transfer a payload of up to 4KB to a client app.

PS: FCM is free. The limit is not mentioned anywhere in the docs.

We will cover 5 important API’s

  1. Send Notification
  2. Create group notification
  3. Add and remove FCM to group notifcation
  4. Create and remove topic subscription
  5. Batch import topic subscription

1.Send Notification:

https://fcm.googleapis.com/fcm/send (POST)

Use below API and pass valid API_KEY in authorization field.

Headers
request body

Request Body

Request body should consist of “data” and “to” below format i use frequently. there is no limit in adding attributes to data json pass here and control like however you want.

{
"data":{
"title":"New Text Message",
"image":"https://firebase.google.com/images/social.png",
"message":"Hello how are you?"
},
"to":"VALID_REGISTRATION_ID"
}

it is possible to send nested objects also like below i have attached “meta”

{
"data":{
"title":"New Text Message",
"image":"https://firebase.google.com/images/social.png",
"message":"Hello how are you?",
"meta":{
"type":"small",
"info":"Search"
}
},
"to":"VALID_REGISTRATION_ID"
}

Response

success response

2.Validate FCM registration id:

https://iid.googleapis.com/iid/info/<VALID_REGISTRATION_ID> (GET)

header

Response:

response body

3.Create Group

With device group messaging, you can send a single message to multiple instances of an app running on devices belonging to a group. Typically, “group” refers a set of different devices that belong to a single user. All devices in a group share a common notification key, which is the token that FCM uses to fan out messages to all devices in the group.

PS: The maximum number of members allowed for a notification key is 20.

https://android.googleapis.com/gcm/notification(POST)

Headers:

Header Parameters

Request Body

To create a group you can pass array of FCM registered token keys on “registration_ids” attribute

The notification_key_name is a name or identifier (e.g., it can be a username) that is unique to a given group. Thenotification_key_name and notification_key are unique to a group of registration tokens. It is important that notification_key_name is unique per client app if you have multiple client apps for the same sender ID. This ensures that messages only go to the intended target app.

Response

A successful request returns a notification_key like the following:

4.Add or Remove FCM to a group

To add or remove devices from an existing group, send a POST request with the operation parameter set to add or remove, and provide the registration tokens for addition or removal.

Request:

request body

Response:

response body

5.Create Topic

Based on the publish/subscribe model, FCM topic messaging allows you to send a message to multiple devices that have opted in to a particular topic. You compose topic messages as needed, and FCM handles routing and delivering the message reliably to the right devices.

For example, users of a local weather forecasting app could opt in to a “severe weather alerts” topic and receive notifications of storms threatening specified areas. Users of a sports app could subscribe to automatic updates in live game scores for their favorite teams.

Some things to keep in mind about topics:

  • Topic messaging supports unlimited topics and subscriptions for each app.
  • Topic messaging is best suited for content such as news, weather, or other publicly available information.
  • Topic messages are optimized for throughput rather than latency. For fast, secure delivery to single devices or small groups of devices, target messages to registration tokens, not topics.
  • If you need to send messages to multiple devices per user, consider device group messaging for those use cases

https://iid.googleapis.com/iid/v1:batchAdd(POST)

Headers

headers
{“to”: “/topics/test2”,“registration_tokens”: [“…”,”…”]}

Note: topic must be prefixed with “topic/<any name>”

https://developers.google.com/instance-id/reference/server

Once topic create send notification using topic name

“to”: “/topics/test2”

To identify how many topics assigned to particular use below endpoint

https://iid.googleapis.com/iid/info/<fcm_token>?details=true(POST)

Response

Response
request body

Response

response body

7. Batch import

https://iid.googleapis.com/iid/v1:batchImport(POST)

batchImport method, you can bulk import existing iOS APNs tokens to Google Cloud Messaging or Firebase Cloud Messaging, mapping them to valid registration tokens. Call the Instance ID service at this endpoint, providing a list of APNs tokens in the JSON body

Request:

Response:

--

--