44

I know that C2DM registrations expire, and you are supposed to periodically refresh the registration ID. Is this the case with GCM? by looking at the following code on the Android GCM guide (shown below), it seems like you only do it once and don't need to refresh, but I dont see that explicitly written anywhere, so I just wanted to check.

final String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) {
  GCMRegistrar.register(this, SENDER_ID);
} else {
  Log.v(TAG, "Already registered");
}
Mohamed Hafez
  • 8,621
  • 7
  • 41
  • 49
  • Similar: http://stackoverflow.com/questions/16838654/handling-registration-id-changes-in-google-cloud-messaging-on-android – trante May 22 '14 at 10:29

2 Answers2

41

EDIT: THIS ANSWER IS WAY OUT OF DATE, I HAVE NO IDEA WHAT THE CURRENT BEHAVIOR IS


I found the answer myself. You don't explicitly need to re-register all the time, just once according to the example in the docs.

Also, unlike previous versions of GCM and C2DM, Google itself does not refresh the registration itself now: once you have the registration id from the initial registration you are good to go, except for one case: you do still need to re-register when the user upgrades to a new version (this case is also handled in the example in the link above):

When an application is updated, it should invalidate its existing registration ID, as it is not guaranteed to work with the new version. Because there is no lifecycle method called when the application is updated, the best way to achieve this validation is by storing the current application version when a registration ID is stored.

Mohamed Hafez
  • 8,621
  • 7
  • 41
  • 49
  • 1
    @AndroidKiller: read thru the docs: http://developer.android.com/google/gcm/index.html – Mohamed Hafez Oct 08 '13 at 19:08
  • hey @MohamedHafez. Your first paragraph comes from C2DM not GCM documentation which means (I believe) that it does not apply to GCM. This makes your answer a bit misleading. Thx – AndroidGecko Feb 27 '14 at 11:26
  • 2
    @AndroidGecko it was in the GCM docs when I first wrote this answer, re-reading the docs now it says the registration procedure has changed (though the old one still works), and it seems that now Google **does not** itself refresh the registration ids now. I've updated the answer to reflect the current version of GCM, good catch:) – Mohamed Hafez Feb 27 '14 at 16:02
10

I think that it is refreshed eventually, yes. From the official docs:

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. 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.

This could happen in a request to GCM from your 3rd-party server, which returns a json response with the error Unregistered Device.

Once this happen, it'll be up to you to refresh the corresponding id's.

http://developer.android.com/guide/google/gcm/gcm.html

Iñigo
  • 12,723
  • 2
  • 31
  • 31
  • So is it save to just check if the token has changed in my `RegistationIntentService`? I can store the token in shared preferences and check every-time to see if it has changed. – Zapnologica Aug 04 '15 at 19:18