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
https://fcm.googleapis.com/fcm/notification Content-Type:application/json Authorization:key=API_KEY project_id:SENDER_ID { "operation": "create", "notification_key_name": "appUser-Chris", "registration_ids": ["4", "8", "15", "16", "23", "42"] }
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
{ "notification_key": "APA91bGHXQBB...9QgnYOEURwm0I3lmyqzk2TXQ" }
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:
https://fcm.googleapis.com/fcm/notification?notification_key_name=appUser-Chris Content-Type:application/json Authorization:key=API_KEY project_id:SENDER_ID {}
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:
{ "operation": "add", "notification_key_name": "appUser-Chris", "notification_key": "APA91bGHXQBB...9QgnYOEURwm0I3lmyqzk2TXQ", "registration_ids": ["51"] }
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:
https://fcm.googleapis.com/fcm/send Content-Type:application/json Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA { "to": "aUniqueKey", "data": { "hello": "This is a Firebase Cloud Messaging Device Group Message!", } }
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:
{ "success":1, "failure":2, "failed_registration_ids":[ "regId1", "regId2" ] }
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:
String to = "a_unique_key"; // the notification key AtomicInteger msgId = new AtomicInteger(); FirebaseMessaging.getInstance().send(new RemoteMessage.Builder(to) .setMessageId(String.valueOf(msgId.get())) .addData("hello", "world") .build());
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