User Notifications - How to recover a lost notification_id from GCM?
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.
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.