14

I'm trying to figure out how to manage FCM device groups from an app server by using the REST API.

AFAIK these are the updated docs: https://firebase.google.com/docs/cloud-messaging/android/device-group#managing_device_groups

Here's what I can already do:

  • Create a new device group with some device tokens
  • Add device tokens to an existing device group

And this is what I just can't figure out how to do since there's no mention of it in the docs:

  • Query whether a device group already exists, based on its notification_key_name.

    Workaround 1: if I try creating a group with a notification_key_name that already exists then I get an error telling me so, but that seems like a very hacky way to find out.

    Workaround 2: Store that information by myself somewhere else.

  • Finding out which device tokens (registration_id) belong to a device group.

    Workaround: as before, store that information by myself somewhere else.

  • Remove device tokens (registration_id) from a device group.

    Workaround: none.

  • Remove a device group.

    Workaround: none.

Thanks!

Josep Sayol
  • 407
  • 1
  • 6
  • 10

2 Answers2

15
  • Query whether a device group already exists, based on its notification_key_name.

Your 2nd workaround is the way to go. You should store it in your App server, same where you also store the Registration Tokens.


  • Finding out which device tokens (registration_id) belong to a device group.

Same as the workaround above. You have to manage these details on your App Server. It is the developer's responsibility to manage these details. Matching the actions if the registration device is removed, you'll have to remove it from your App Server as well.


  • Remove device tokens (registration_id) from a device group.

I'm not sure what you need here. The documentation have details on removing registration tokens from the device group:

Adding and removing devices from a device 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.

Note: If you remove all existing registration tokens from a device group, FCM deletes the device group.

HTTP POST request

For example, to add a device with the registration ID 51 to appUser-Chris, you would send this request:

{
   "operation": "add",
   "notification_key_name": "appUser-Chris",
   "notification_key": "APA91bGHXQBB...9QgnYOEURwm0I3lmyqzk2TXQ",
   "registration_ids": ["51"]
}

Response format

A successful request to either add or remove a device returns a notification_key like the following:

{
   "notification_key": "APA91bGHXQBB...9QgnYOEURwm0I3lmyqzk2TXQ"
}

Note: notification_key_name is not required for adding/removing registration tokens, but including it protects you against accidentally using the incorrect notification_key.


  • Remove a device group.

From the note in the docs above:

Note: If you remove all existing registration tokens from a device group, FCM deletes the device group.

AL.
  • 36,815
  • 10
  • 142
  • 281
  • 1
    Similar (and possibly helpful) post [here](http://stackoverflow.com/q/42247765/4625829). – AL. Mar 01 '17 at 02:14
  • Thanks AL! Really helpful. I can't believe I missed the parts about removing devices and device groups in the docs. That's what I get for skimming through it instead of reading it thoroughly, sorry! – Josep Sayol Mar 01 '17 at 08:55
  • In this part "send a POST request with the operation parameter set to add or remove", would you mind including the resulting endpoint for an add operation? – moondaisy May 29 '17 at 17:50
  • 1
    @moondaisy Hi. The endpoint used for Managing the device groups is pretty much the same in any operation: [`https://android.googleapis.com/gcm/notification`](https://firebase.google.com/docs/cloud-messaging/android/device-group#managing-device-groups-on-the-app-server). – AL. May 30 '17 at 01:45
1

To add another option to the first question, not sure if this was added by FCM after the accepted answer but, in the Managing device groups on the app server section in the Android (and IOS) documentation, there is a part on Retrieving a notification key using a GET request to

https://fcm.googleapis.com/fcm/notification?notification_key_name=appUser-Chris

This returns

{ "error": "notification_key not found" }

if the device group does not exist, or the notification key, otherwise.

relie
  • 11
  • 1