why do gcm docs recommend invalidating registration on app update?

不打扰是莪最后的温柔 提交于 2019-12-04 19:29:16


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 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. Then when the application is started, compare the stored value with the current application version. If they do not match, invalidate the stored data and start the registration process again.

When the docs state that "it is not guaranteed to work with the new version" is that a GCM limitation or are they speculating about potential changes in my app's behavior from version to version?

From the app side I can more-or-less guarantee that successive versions will function properly with respect to GCM and whatever app-specific message format I concoct. Do I still need to re-register?

If so, which should I use to detect a "new version": version code or version name? My understanding is that these are "free form" and the app developer sets them to whatever values he chooses. So, what if I put an app update in the store but don't change versionName or versionCode; would I need to re-register with GCM?

It seems like what GCM actually wants is for the app to re-register each time a new installation is launched for the first time (and each time it's successively launched until registration is complete), regardless of the values in versionName and versionCode. Is that an accurate statement?


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.


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()) {
        } 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.