Device Group Messaging with Firebase Cloud Messaging

Device Group Messaging with Firebase Cloud Messaging

Device Group Messaging allows you to send a single message to all devices that belong to a single user, otherwise known as a group. Quite frankly, a group can be the devices that belong to more than a single user, we are however limited to 20 devices per group, so that sucks.

Use Cases

Why though? Why would you need this?

  • Well, to be honest, I’m not quite sure myself. Care to enlighten me?

Well we’re doing it anyway.

Before anything, we need to create the device group. Most work from creating to managing the group is done via the app server so that means we’ll be diving well deep into HTTP POST requests.

Creating the Device Group

To create, we send a request like this to https://fcm.googleapis.com/fcm/notification

Let’s analyse a few important key bits here:

notification_key_name: the group’s identifier. It should be unique, like a username

registration_ids: the devices to be included in the group

Getting a Response

If the request is successful, you’ll get a response like this. Save the notification key as you’ll need it in future operations.

Retrieving a Notification Key

You may still retrieve notification keys for existing groups with a GET request like this one:

Each time, the key will be different, but it will all be valid.

Adding/Removing Devices from Groups

To add or remove, send a POST request like this one:

Note the operation field. This should be “add” or “remove” depending on what you need.

Note that removing all devices from a group will automatically delete that group.

Sending Downstream Messages to Groups

Send a POST request to  https://fcm.googleapis.com/fcm/send like this one:

to: This should be the notification key of the device group

As for the other fields, constructing the message can be done as you would any other Cloud Message. You can have your notification payload, data payload, all that jargon.

HTTP Response

Regardless of the result, you’ll get a response like this:

In the case of any failures, the app server will retry with backoff between retries.

Sending Upstream Messages from Client Devices (Android)

Yes, devices actually CAN send messages to other device groups. This is how its done:

I’ll be honest, I was pleasantly surprised to find out this was possible.

Read More

Check out the other parts of my Firebase Cloud Messaging series:

1. Getting Started with Firebase Cloud Messaging on Android

2. Topic Messaging with Firebase Cloud Messaging on Android

4. Constructing POST HTTP Firebase Cloud Messages for dummies and YOU