4

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
NickT
  • 23,844
  • 11
  • 78
  • 121
  • @Arpit Garg: It doesn't answer my question. I know HOW to check but why would I? I don't open the email application to check for messages or for it having an expired 'id',it just sits there and tells me when one arrives. GCM should behave in the same way. – NickT Oct 10 '12 at 17:38

1 Answers1

1

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.

Mohsen Afshin
  • 13,273
  • 10
  • 65
  • 90
  • The whole point of the GCM from a client point of view is it remains invisible to the client until a message arrives. This is triggered by the intent service which has a receiver registered (like a BroadcastReceiver). That receiver can't do anything unless it receives an intent from someone sending a message. The whole thing requires no user interaction. Are you saying that a user needs to remember to run the app to check the reg_id every now and then? The receiver isn't going to do it for the user. – NickT Oct 10 '12 at 19:49
  • @NickT, I edited my answer. Once you register the REGISTRATION intent, the result of Register, Unregister, Expiration,... and anything related to your registration_id will be sent to this intent, you just handle the messages received by REGISTRATION intent. – Mohsen Afshin Oct 10 '12 at 20:56
  • That's fine if Google actually send something to the REGISTRATION intent when the id is expired and still works (otherwise you wouldn't get the message) and the new id is in the canonical id field. I.e there is a period of grace during which both ids are valid. The question is do you absolutely know that this is what they do? – NickT Oct 10 '12 at 21:18