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

前端 未结 2 1190
野的像风
野的像风 2020-12-06 14:20

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

相关标签:
2条回答
  • 2020-12-06 15:05

    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.
    0 讨论(0)
  • 2020-12-06 15:20

    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
    0 讨论(0)
提交回复
热议问题