简体   繁体   中英

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

I am using GoogleCloudMessaging to implement notification feature in my app. But I have a problem that when I uninstall my app and install it again , I got 2 different registration ids , which leads to duplicate notifications to my app .

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

public String register (String... senderIds)

Register the application for GCM and return the registration ID. You must call this once, when your application is installed, and send the returned registration ID to the server.

Repeated calls to this method will 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 is behaving well, everything should work normally. However, if a bug in the application triggers multiple registrations for the same device, it can be hard to reconcile state and you might end up with duplicate messages.

So how can I make this consistent to all the devices? My (third-party) server only store the registration ids, which will be used to send notifications. Now, here come "Canonical IDs" and everything just got so much complicated!

Does this mean that I have to send a unique identify number for each device when I do the register?

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

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

Yes, registering the app after it is re-installed may give you a new registration ID. You can handle it either in the server side or the client side :

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

  • Server Side : Whenever you get a 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 do both, since the second only fixes the problem after you already get a duplicate message, and the first is not full proof (since external storage may be deleted or unavailable).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM