Reputation: 23873
I've put together a skeleton client/server GCM app that accepts and displays messages on my Android phone from my rough and ready server. I say 'skeleton 'and 'rough and ready' because my app's registration_id is hard coded into the server side as the value I captured in the debugger when I started the sample Android app and registered the app/device combination on the GCM server. I was about to start working on providing a read/write mechanism for the two sides to access a simple database on the server to store the reg_id/device combinations when I got to thinking:
I know this reg_id can expire at any time, because Google say it can. It's clear from the docs how to check that the id is still OK IF, and it's a big IF, I start the app again. However, why should I, or any other user, start it again? The GCMIntentService, registered in the manifest is sitting there picking up messages as required and there's no need to run the app again for any reason. So when the id expires, I'll stop getting messages - the server might know from the return code, but won't be able to inform my app - no recovery unless I run the app again and go through its onCreate()
and presumably registerClient()
methods.
So what happens when the id expires? I don't see that any of the on.. methods in the IntentService will execute. Will Google themselves send a normal message saying it's about to expire? Do I need to write an alarm task to run daily and check the id?
Upvotes: 4
Views: 2641
Reputation: 13436
According to GCM:
An existing registration ID may cease to be valid in a number of scenarios, including:
- If the application manually unregisters by issuing a com.google.android.c2dm.intent.UNREGISTER intent.
- If the application is automatically unregistered, which can happen (but is not guaranteed) if the user uninstalls the application. If the registration ID expires. Google might decide to refresh registration IDs.
- If the application is updated but the new version does not have a broadcast receiver configured to receive com.google.android.c2dm.intent.RECEIVE intents.
Consider that registration related responses will fire"com.google.android.c2dm.intent.REGISTRATION
" intent. So you should take care of Registration messages sent from GCM.
Updated
GCM doc says:
If the registration ID expires. Google might decide to refresh registration IDs. For all these cases, you should remove this registration ID from the 3rd-party server and stop using it to send messages. Happens when error code is NotRegistered.
Considering the above fact as the truth, then on the your server you should handle expired IDs and set a flag on them in your db, the next time your android client check your server(finally you must do a periodical check) or you call isRegisteredOnServer
on GCMRegistrar class and got a false result, you should fire REGISTRATION intent in order to get a new ID.
Upvotes: 1