问题
I'm struggling with the next issue about GCM. I read the document (GCM document) and saw the example by Google how to implement the GCM client within Android app but in the entire document there is no reference to expiration of Registration Id.
I saw that question but the example there is not following the last update by Google to the GCM Demo App
My questions are:
- Does a Regisration Id ever expire?
- If a Regisration Id expires, how can I know that (is there any event for that)?
Thanks in advance.
回答1:
I'll expand on my comment.
Based on all I read in the GCM docs and examples, you should only re-register to GCM after a new version of your app is installed on a device. You don't have to handle expiration of the registration ID.
Even if for some reason GCM decide to assign a new Registration ID to your app on a given device, and neither your app nor your server are aware of it, the next time your server pushes a notification to the device (using the old Registration ID), it will get the new Registration ID as Canonical Registration ID in the response from Google, and will be able to update its DB accordingly. I suggest that you assign each device your own unique ID (assigned by your server), and receive that ID from the device whenever it sends you the Registration ID. This way, if the Registration ID is changed by the server (as a result of canonical registration ID response from Google), and at some later point the app sends you for some reason the old registration ID, you'll know that both IDs refer to the same device.
The only case I can think of when it's beneficial to set an artificial timeout in your app, and re-register to GCM when that timeout expires (and send it to your server), is if your server is so un-reliable that it might lose its DB of Registration IDs (I've seen questions on SO by people this actually happened to). This will allow the server to gradually recover the lost Registration ID.
来源:https://stackoverflow.com/questions/19568785/gcm-regisration-id-expiration-time