why do gcm docs recommend invalidating registration on app update?

前端 未结 2 1074
渐次进展
渐次进展 2021-02-14 15:24

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

2条回答
  •  星月不相逢
    2021-02-14 16:18

    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.");
            }
    

提交回复
热议问题