4

Assuming that one has created a device group in Firebase Cloud Messaging, is there a way to retrieve an existing notification_key for a device group after it's been created?

Is there a way to look up the notification_key based on either notification key name or by registration id?

It seems the notification key is only returned on the create method and if the key is ever lost or errors on saving to the the database - it would be impossible to add another registration id to the the key name without a notification_key since it already exists.

MonkeyBonkey
  • 46,433
  • 78
  • 254
  • 460

2 Answers2

7

You can retrieve the notification_key for a device group if you know the notification_key_name that was used when you created it. Ref: https://firebase.google.com/docs/cloud-messaging/android/device-group

Use this:

https://android.googleapis.com/gcm/notification?notification‌​_key_name=your-key-n‌​ame

Ref: https://groups.google.com/forum/#!topic/firebase-talk/ytovugx8XNs

For example:

let options = {
    url: 'https://android.googleapis.com/gcm/notification?notification_key_name=the_name',
    method: 'GET',
    headers: {
        "Content-Type": "application/json",
        "Authorization": "key=" + authorizationKey,
        "project_id": projectId
    }
};

request(options, function (error, response, body) {
    if (!error) {
        res.json(body);
    }
    else {
        res.json(error);
    }
});

One thing I found when using this call was that the notification_key returned was always different, but I was able to use it successfully to add or remove registration_ids.

I'll add some additional information from comments I made earlier:

  1. I've had quite a bit of trouble with device groups. The only way to delete a device group is to remove all the notifications keys it contains. If you lose track of a notification key that you have added to the device group then deleting that device group is impossible, as there is no way currently to get a list of the notification keys in a device group.
  2. The device group is a very good mechanism for sending messages to multiple devices (see @AL.'s comment for another method). I also store in the database the registration_ids for each device group. The reason for that is if the user deletes their account for my app I also delete their device group so that the device group name can be reused.
camden_kid
  • 12,591
  • 11
  • 52
  • 88
  • 1
    I tried the http request you posted - but got a 400 error `INVALID_PARAMETERS` – MonkeyBonkey Mar 04 '17 at 12:26
  • @MonkeyBonkey I just tested it on my side at it works. See updated answer for more details. – camden_kid Mar 04 '17 at 13:56
  • it was an error on my postman client - was double escaping params - I got it now - thanks! – MonkeyBonkey Mar 04 '17 at 16:05
  • Interesting. I wasn't aware that you can retrieve the key with just the name. Though I'm a bit at loss on what scenario one would lose the `notification_key`, but be able to keep the `notification_key_name`. – AL. Mar 10 '17 at 01:51
  • 1
    @AL. It's actually helped me in some instances when I've accidentally deleted the notification_key from my database when testing and resetting. In those situations I could construct the notification_key_name and get the notification_key back. – camden_kid Mar 10 '17 at 09:49
  • I guess it can happen during testing. Didn't cross my mind. Was thinking about in a prod scenario. Anyways, good catch. Cheers! :) – AL. Mar 10 '17 at 10:09
  • 1
    Does it still work? I keep getting http-400 with INVALID_PARAMETERS error message – Richeek Sep 13 '17 at 12:52
1

There is currently no API to retrieve the Device Groups (notification_keys) associated with a given Registration Token. AFAIK, managing/mapping relationships of Device Groups and its associated registration tokens are the developers responsibility.

For your scenario, I would suggest to temporarily store the notification_key until it is successfully stored in your App Server.

Some possibly helpful posts:

Community
  • 1
  • 1
AL.
  • 36,815
  • 10
  • 142
  • 281
  • How would you handle the scenario where the notification_key gets lost - is there a way to reset the device group entirely as that would block creating new device groups with that device key name forever. – MonkeyBonkey Mar 03 '17 at 11:54
  • @MonkeyBonkey I've had quite a bit of trouble with device groups. The only way to delete a device group is to remove all the notifications keys it contains. If you lose track of a notification key that you have added to the device group then deleting that device group is impossible, as there is no way currently to get a list of the notification keys in a device group. – camden_kid Mar 03 '17 at 11:58
  • @camden_kid do you think it's better for me to keep an array of registration_ids myself in my own data store instead, rather than use device groups? – MonkeyBonkey Mar 03 '17 at 13:15
  • 1
    @MonkeyBonkey I do both. The device group is the best mechanism to send a message to multiple devices so I use that. I also store in the database the registration_ids for each device group. The reason for that is if the user deletes their account I also delete their device group so that the device group name can be reused. – camden_kid Mar 03 '17 at 13:22
  • I wouldn't go as far as saying that Device Groups is the *best* for sending to multiple devices. It still depends on the use-case. If the devices are from the same user, the advised is to use Device Groups. Advised, but not required. If you don't prefer to do the extra work (managing/mapping the relationships), go for topics. A sample way for this is having a userId of some sort as the topic name, then subscribe/unsubscribe the corresponding token(s) depending on what you need. I can put in more details once I get back or you look around my answers with an FCM tag. Cheers! :) – AL. Mar 03 '17 at 15:29
  • 1
    @AL. It's true there is some extra work required with device groups. I'll edit my answer. Thanks. – camden_kid Mar 10 '17 at 09:52