Register GoogleCloudMessaging # returns different IDs in the same app / device?

I am using GoogleCloudMessaging to implement notification functionality in my application. But I have a problem: when I uninstall my application and install it again , I got 2 different registration IDs , resulting in duplicate notifications in my strong> application .

In the doc they said, "Repeated calls to this method will return the original registration ID"

public String register (String ... senderIds)

Register your app for GCM and return the registration ID. You have to call this once your app is installed and send the returned registration ID to the server.

Repeated calls to this method return the original registration ID.

But in another document, they came up with something called "Canonical IDs":

Canonical IDs On the server side, as long as the application behaves well, everything should work fine. However, if a bug in the app triggers multiple registrations for the same device, it can be tricky and you might get duplicate messages.

So how can I make this consistent across all devices? My (third party) server stores registration IDs that will be used to send notifications. Now the "Canonical Identifiers" came in and things got so complicated!

Does this mean that I have to send a unique ID for each device when I register?

This is the block of code I used to register the device with GoogleCloudMessaging:

try {
    if (gcm == null) {
        gcm = GoogleCloudMessaging.getInstance(context);
    regid = gcm.register(SENDER_ID);
    Log.d(TAG, "########################################");
    Log.d(TAG, "Current Device Registration ID is: " + regid);
    Map<String, String> data = new HashMap<String, String>();
    data.put("regId", regid);
    try {, data);
        Log.d(TAG, "ID registered: " + regid);
    } catch (Exception e) {
} catch (IOException ex) {
    msg = "Error :" + ex.getMessage();
    Log.d(TAG, msg);



source to share

1 answer

Yes, registering an app after reinstalling it may give you a new registration ID. You can handle it either server side or client side:

  • Client Side: Assign a unique instance ID for each instance of your application. Store this identifier in external storage so that it is not deleted when the application is uninstalled. Send this ID to your server along with the registration ID, and replace the old registration ID with the new one if you find an old registration ID in your DB associated with the same instance ID.

  • Server side: whenever you get the canonical registration id in a response from GCM, remove the old registration id from your DB (and insert the canonical registration id if it's not already in your DB).

I suggest you both, as the second only fixes the problem after you've already received the duplicate message, and the first is not complete proof (since the external storage might be deleted or not available).



All Articles