Unregistering and re-registering for GCM messages causes two regId's to be valid. Is this as intended?

android, google-cloud-messaging, push-notification

Solution

When you un-register, you should send the old registration ID to your server and remove it from your DB.

Assuming you failed to do step 1, if after registering and getting the new registration ID you send a message with the old registration ID, the response from Google will contain a Canonical Registration ID (which is the new registration ID). This response indicates that your server should delete the old registration ID and use only the new one.

Problem

I have noticed some strange behaviour when doing registering/unregistering for GCM messages on an Android devices. Observe the following use case from the perspective of the client device: - Register for GCM -- ID A assigned - Unregister - Register for GCM -- ID B assigned If, after step 2, the server attempts to send a message to ID A, it will receive a `NotRegistered` error, as documented and expected. But now the strange part: After step 3, both ID A and B are valid IDs! Both IDs will trigger the Intent receiver on the device, resulting in two messages to the app. Is this behaviour as expeced or am I doing something wrong? This is my code to register and unregister, triggered from `onCreate()` on the first activity launching on my app: ``` public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); unregister(getApplicationContext()); register(getApplicationContext()); } /** Registers this device for GCM messages */ public static void register(Context context) { GCMRegistrar.checkDevice(context); GCMRegistrar.checkManifest(context); String regId = GCMRegistrar.getRegistrationId(context); if (regId.equals("")) { GCMRegistrar.register(context, SENDER_ID); } else { storeRegId(regId); // Also notifies back-end } } public void unregister(Context context) { GCMRegistrar.unregister(context); } ``` Note 1: I've only included the unregister()-call for debugging purposes. My app usually stays registered for "for life" (I also want to receive GCM messages while suspended and terminated), but I still want to figure out the cause of this behaviour as I'm not sure if unregistering is the only case where the IDs are regenerated. What if the user uninstalls and reinstalls the app? I want a bullet proof system - my users shall never receive the same GCM message twice. Note 2: The problem is pretty similar to this, except that I am indeed registering with `getApplicationContext()` as the answer suggests.

Original source

Related problems