From the GCM docs:
When an application is updated, it should invalidate its existing registration ID, as it is not guaranteed to work with the new versi
I don't remember where we've read it, but it came to our attention that when a device gets a push while an app is not installed, Google will invalidate the registration id.
This makes sense if the app is really uninstalled, but if the device was actually in the middle on an update, it quickly uninstalls and re-installs, so google might mistakenly think the registration needs to be invalidated.
The solution seems like to re-register on the first launch after an update, to guarantee your app registration id is active.
Version code is indeed a freely selected number, but you must increase it on every new version you publish to google play, so you can check if that number has changed, and know your app had been updated and you need to refresh the registration.
EDIT:
This is also relevant to C2DM
's successor GCM
, with a lot more docs explaining this behavior and how to properly write code.
See: http://developer.android.com/google/gcm/client.html with all the details.
Specifically this code, where getRegistrationId
will return an empty string in case the version code changed forcing the client to register again:
if (checkPlayServices()) {
gcm = GoogleCloudMessaging.getInstance(this);
regid = getRegistrationId(context);
if (regid.isEmpty()) {
registerInBackground();
}
} else {
Log.i(TAG, "No valid Google Play Services APK found.");
}
I would use the Version Code to detect the app update. The Version Code is forced to change every time you submit a new version to the Google Play store, hence you can rely on it to detect the app's version.