Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to retrieve an existing notification_key based on a registration id or a notification key name?

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.

like image 861
MonkeyBonkey Avatar asked Feb 04 '23 23:02

MonkeyBonkey


2 Answers

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.
like image 132
camden_kid Avatar answered Feb 08 '23 15:02

camden_kid


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:

  • Firebase Cloud Messaging - Managing Registration Tokens
  • Managing FCM device groups
like image 39
AL. Avatar answered Feb 08 '23 15:02

AL.