0

Is there something wrong in the fact that every time I log in to my android application, the first thing I do is to register to GCM. I know it's not necessary to do this, but in order to check if the registration id is refreshed to a new one or it s still the same, I plan to re-register.

final String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) {
  GCMRegistrar.register(this, SENDER_ID);
} else {
  Log.v(TAG, "Already registered");
}

I would also like to know if by re-registering to GCM I am somehow forcing the registrationID to expire.

Thank you!!!

just ME
  • 1,817
  • 6
  • 32
  • 53

1 Answers1

2

Part 1:

In order to check if the registration id is refreshed to a new one or it is still the same, I plan to re-register.

Now, if you see the second point under the heading Enable GCM on Architectural Overview page, it says:

Note that Google may periodically refresh the registration ID, so you should design your Android application with the understanding that the com.google.android.c2dm.intent.REGISTRATION intent may be called multiple times. Your Android application needs to be able to respond accordingly.

So, Google automatically sends you this broadcast, when it renews the ID. You might not necessarily send check the emptyness of the ID. When Google changes the ID, then also it won't be empty, right? So if you want to handle the renewal/refreshing of the ID then you may do the following:

You should have a Broadcast Listener which could handle com.google.android.c2dm.intent.REGISTRATION intent, which Google send to the app when it has to refresh the registration ID. The broadcast receiver will have the onReceive method with an Intent. From the intent you can get the Bundle using which you can extract the new registration ID from Google. You can save that and send it to the 3rd part server to replace your previous registered ID for that user.

Also you may see this answer on the question In GoogleCloudMessaging API, how to handle the renewal or expiration of registration ID?.

Part 2: Answering your second part of question:

You may want to read Unregistration here. The docs says If you unregister and then re-register, GCM may return the same ID or a different ID—there's no guarantee either way.

I think that whenever an ID will be about to expire/renew, Google will send you the new ID. Then the response from Google will contain a Canonical Registration ID (which is the new registration ID). This response indicates that your server should delete the old registration ID and use only the new one. ( Source : Answer at Unregistering and re-registering for GCM messages causes two regId's to be valid. Is this as intended? question by @Eran)

Hope this helps you understand how to handle it.

Community
  • 1
  • 1
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
  • Thank you. I think I skipped the part where the GCM sends to the client a broadcast with the nre registration id. Can you please write or derect me to an example of how to catch this broadcast message?:) Than you! – just ME Aug 23 '13 at 09:13
  • Sorry I could not find a piece of code right now but if you read comments on http://stackoverflow.com/questions/17891399/should-applications-call-gcm-register-every-seven-days-to-ensure-valid-registr/17892487#17892487 , it should be straight forward just like you implement other Broadcast receiver. The actual docs don't provide code sniplets for this but just have mentioned the above things. – Shobhit Puri Aug 23 '13 at 09:22
  • @justME You don't need to define a new `BroadcastReceiver`. You can handle `` in the `GCMBroadcastReciever` only. Have it inside `intent-filter`. Whenever you receive the Broadcast, check if the `action` is `com.google.android.c2dm.intent.REGISTRATION`. Then you may update the ID by extracting it from the intent. Hope its a bit clear now. – Shobhit Puri Sep 02 '13 at 08:36