I generate notification_key
s as described here.
Suppose that due to some error I fail to store it, so I try to re-register.
This time I receive a 400 error with the message "notification_key already exists"
.
This looks odd, especially compared to registration of a device to GCM, where you can register as many times as you want and always get the same Registration ID with status 200.
Now I can't register again, and I also can't unregister, because I don't have the notification_id
.
Is there any way to get the previously generated notification_key
from GCM?
Or is the only way to register again with a different notification_key_name
?
Based on the docs, there is no way to get from GCM the notification_key
of an existing notification_key_name
. If you think about it, it makes sense that trying to create a new notification_key
for an existing notification_key_name
would give you an error, since if it wasn't the case, you might be accidentally overwriting the Registration IDs of an existing notification_key
if you happen to supply an existing notification_key_name
by mistake.
You are comparing this to registering a device to GCM multiple times, each time getting the same Registration ID, but it's not a similar situation. When you register a device to GCM, GCM has a way to identify the device and know that it is already registered and return the same Registration ID. With user notifications, it only has the notification_key_name
that you supplied, and there's nothing stopping you from using the same notification_key_name
for multiple users. That is, there is something stopping you - the error you got when trying to create a notification_key
with a previously used notification_key_name
.
An easy way to overcome your problem is to treat notification_key_name
as a unique identifier generated by your server. If you don't have a notification_key
for a certain user (either because it's a new user or because you failed to store the notification_key
you previously got from Google), you generate a new unique notification_key_name
and use it to create a new notification_key
. You don't have to care about the old notification_key
that you failed to store.
Finally you store both the notification_key
and notification_key_name
in a table that contains the user id.
I can't find any documentation about it but it's now possible to recover a notification_key
for a device group by doing a GET
request to https://fcm.googleapis.com/fcm/notification?notification_key_name=my_notification_key_name with the headers they require: Authorization: key=my_key
, Content-Type: application/json
and project_id: my_id
.
You will get a response like {
"notification_key": "lost_key"
}
But be aware that returned notification_key is not the same as original, but you can use both of them.
If you happen to know all the registered registration ids in the device group. Then delete them all and the device group will be deleted too. Afterwards you can create a device group with the before used notification_key_name.