omerjerk
omerjerk

Reputation: 313

Handle registration id changes in GCM client

I'm reading the documentation here : http://developer.android.com/google/gcm/gs.html

See this method :

private String getRegistrationId(Context context) {
    final SharedPreferences prefs = getGCMPreferences(context);
    String registrationId = prefs.getString(PROPERTY_REG_ID, "");
    if (registrationId.length() == 0) {
        Log.v(TAG, "Registration not found.");
        return "";
    }
    // check if app was updated; if so, it must clear registration id to
    // avoid a race condition if GCM sends a message
    int registeredVersion = prefs.getInt(PROPERTY_APP_VERSION, Integer.MIN_VALUE);
    int currentVersion = getAppVersion(context);
    if (registeredVersion != currentVersion || isRegistrationExpired()) {
        Log.v(TAG, "App version changed or registration expired.");
        return "";
    }
    return registrationId;
}

It returns empty String when there's no id saved in the SharedPreferences or version of the app is changed or the registration expired.

And in all these three cases a method registerBackground() is called. This method makes a HTTP request to my web server and the server stores the registration id in database.

But if this happens every version update or registration expiration will create a new row inside the TABLE which is on my server.

Is there a way to get when the registration id change event happens on client side ? Because if this happens I'll update the row in the TABLE .

Upvotes: 2

Views: 1241

Answers (2)

Eran
Eran

Reputation: 393771

You can change the code slightly to achieve what you want.

When the old registration id stored in the shared preferences expires, store it somewhere. Then, after getting the new registration id from Google, send both the old and new ids to your server, and update the DB instead of creating a new row.

Upvotes: 1

Sunil Kumar
Sunil Kumar

Reputation: 7092

It gives nullPointerException if registration id has null. Because you are trying to get length. .

if (registrationId==null) {
        Log.v(TAG, "Registration not found.");
        return "";
    }
else
{
  // try to do 
}
}

Upvotes: 0

Related Questions