User Notifications - How to recover a lost notification_id from GCM?

安稳与你 提交于 2019-11-30 07:11:58

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.

Zalastax

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!