Reputation: 39681
I'm taking a look at GCM, I'm not sure what we need to do in the case of application update. The doc says:
"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."
So what should that look like? Something like:
public class MyActivity extends Activity {
@Override
public void onCreate(...) {
if (we are a new app version) {
// calling register() force-starts the process of getting a new
// gcm token?
GCMRegistrar.register(context, SENDER_ID);
saveLastVersionUpdateCodeToDisk();
}
}
so we just need to make sure we call GCMRegistrar.register() again ourselves in case we're a new app version?
Thanks
Upvotes: 1
Views: 1217
Reputation: 34006
Regarding to official documents' examples, one should check whether registration ID is created in current app version. If app is registered with older version, it must be registered again.
http://developer.android.com/google/gcm/client.html
Note that if app is updated then registration id will return as empty, so app will be registered 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.");
}
private String getRegistrationId(Context context) {
final SharedPreferences prefs = getGCMPreferences(context);
String registrationId = prefs.getString(PROPERTY_REG_ID, "");
if (registrationId.isEmpty()) {
Log.i(TAG, "Registration not found.");
return "";
}
// Check if app was updated; if so, it must clear the registration ID
// since the existing regID is not guaranteed to work with the new
// app version.
int registeredVersion = prefs.getInt(PROPERTY_APP_VERSION, Integer.MIN_VALUE);
int currentVersion = getAppVersion(context);
if (registeredVersion != currentVersion) {
Log.i(TAG, "App version changed.");
return "";
}
return registrationId;
}
Upvotes: 0
Reputation: 1085
This question is fairly old, but this is the code I found for GCMRegistrar.getRegistrationId(Context context)
in the helper class source code.
Short answer: GCM code checks to see if the app is updated. You don't have to worry about it as long you call this method and make a call to register with GCM if the return value of this method is blank.
public static String getRegistrationId(Context context) {
final SharedPreferences prefs = getGCMPreferences(context);
String registrationId = prefs.getString(PROPERTY_REG_ID, "");
// check if app was updated; if so, it must clear registration id to
// avoid a race condition if GCM sends a message
int oldVersion = prefs.getInt(PROPERTY_APP_VERSION, Integer.MIN_VALUE);
int newVersion = getAppVersion(context);
if (oldVersion != Integer.MIN_VALUE && oldVersion != newVersion) {
Log.v(TAG, "App version changed from " + oldVersion + " to " +
newVersion + "; resetting registration id");
clearRegistrationId(context);
registrationId = "";
}
return registrationId;
}
Upvotes: 1
Reputation: 844
Yes, you should call GCMRegistrar.register again and in your broadcast receiver make sure to update your server with the new id.
Upvotes: 1