User Notifications – How to recover a lost notification_id from GCM? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of User Notifications – How to recover a lost notification_id from GCM? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I generate notification_keys 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.

We are here to answer your question about User Notifications – How to recover a lost notification_id from GCM? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji