Firebase Cloud Messaging: Notification_key invalid

2019-05-13 23:37发布

问题:

I was playing around with adding and removing device tokens from an FCM device group, and I noticed that if I remove a device from a group, and even if another device exists in that group, FCM cancels the notification_key (detected through trying to reuse the same key to add a new device). This is very bad for me, because it means I can't add new devices to the same group from which a device was removed but others are still there.

Here is some code to confirm this (please filling the appropriate values):

1. Create device group with one device.

curl -X POST \
  https://android.googleapis.com/gcm/notification \
  -H 'authorization: key=YOUR_KEY' \
  -H 'cache-control: no-cache' \
  -H 'content-type: application/json' \
  -H 'project_id: YOUR_PROJECT_ID' \
  -d '{
   "operation": "create",
   "notification_key_name": "some-group-identifier",
   "registration_ids": ["device-token-1"]
}'

This returns the notification key, which I must feed to the next call where we add a new device.

{
  "notification_key": "my-new-notification-key"
}

2. Add new device to the existing group.

curl -X POST \
  https://android.googleapis.com/gcm/notification \
  -H 'authorization: key=YOUR_KEY' \
  -H 'cache-control: no-cache' \
  -H 'content-type: application/json' \
  -H 'project_id: YOUR_PROJECT_ID' \
  -d '{
   "operation": "add",
   "notification_key_name": "some-group-identifier",
   "notification_key": "my-new-notification-key",
   "registration_ids": ["device-token-2"]
}'

Response is same as (1).

3. Remove this last device from group.

curl -X POST \
  https://android.googleapis.com/gcm/notification \
  -H 'authorization: key=YOUR_KEY' \
  -H 'cache-control: no-cache' \
  -H 'content-type: application/json' \
  -H 'project_id: YOUR_PROJECT_ID' \
  -d '{
   "operation": "remove",
   "notification_key_name": "some-group-identifier",
   "notification_key": "my-new-notification-key",
   "registration_ids": ["device-token-2"]
}'

Success. Same response as (1) and (2).

4. Attempt to add a new device to the same group.

curl -X POST \
  https://android.googleapis.com/gcm/notification \
  -H 'authorization: key=YOUR_KEY' \
  -H 'cache-control: no-cache' \
  -H 'content-type: application/json' \
  -H 'project_id: YOUR_PROJECT_ID' \
  -d '{
   "operation": "add",
   "notification_key_name": "some-group-identifier",
   "notification_key": "my-new-notification-key",
   "registration_ids": ["device-token-3"]
}'

FAIL. Response is 400, with {"error": "notification_key not found"}. I can't but think this is a bug? Otherwise how are we expected to dynamically resize a group of devices?

回答1:

It turns out, the registration_ids I was using were from the same chrome app, even when I changed the code of the chrome app thinking this will make it appear as a new one.

Only after creating THREE chrome extensions and generating a registration_id for each did FCM start treating them as separate apps. After this it all worked perfectly.

Thanks @MaX for the help.