I'm having a slight issue with device registration management on my client/server for push notifications.
Problem
The problem that I'm having is that when I uninstall the application and re install it the application returns an empty string for the registration id (GCMRegistrar.getRegistrationId(this)
) which I re-register the device with my server. The problem is that I get a new, different registration id (sometimes) and BOTH WORK! So I don't know how server side to know whether it's for the same device. I should also note I'm not changing the application version. Do any changes to the manifest trigger a new registration id to be issued?
On the Android side, I do the following:
/** * Registers this android device with GCM */ private void registerDeviceWithGCM() { GCMRegistrar.checkDevice(this); GCMRegistrar.checkManifest(this); String regId = GCMRegistrar.getRegistrationId(this); if (regId.equals("")) { log.debug("Registering application with GCM"); GCMRegistrar.register(this, ApplicationData.SENDER_ID); } else { log.debug("Already registered: " + regId); deviceRegistrationService.updateServerRegistrationData(this, regId); } }
In my GCMIntentService.java
I do the following:
/** * Triggered upon new device registration and updates registration info with the server * * @param context context received from * @param regId device's registration id */ @Override protected void onRegistered(Context context, String regId) { Log.d(TAG, "Registering: " + regId); Intent intent = new Intent(RegistrationReceiver.REGISTRATION_INTENT); intent.putExtra(RegistrationReceiver.REGISTRATION_ID, regId); context.sendBroadcast(intent); }
In my RegistrationReceiver.java
I have the following:
/** * Triggers the device registration with cirrus * * @param context unused * @param intent used to get registration id */ @Override public void handleReceive(Context context, Intent intent) { log.debug("Received registration with intent action: " + intent.getAction()); if (intent.getAction().equals(REGISTRATION_INTENT)) { String regId = intent.getStringExtra(REGISTRATION_ID); log.debug("Received registration intent with registration id: " + regId); deviceRegistrationService.updateServerRegistrationData(loginActivity, regId); } else if (intent.getAction().equals(REGISTRATION_FAILED_INTENT)) { log.debug("Received registration failed intent, displaying error message"); showRegistrationFailedMessage(intent); } }
Again, the problem here is that I have two ore more registration id's that all work (it wouldn't be a problem if I the old one simply didn't work when trying to post a message from the server as I can simply clean that up).
Sometimes Google changes the registration ID and you'll have multiple IDs associated. The server that sends the notification (your server) has to update the database with the new ID.
For more info check this document:
http://developer.android.com/google/gcm/adv.html
That says: