6

I am working on an android project which uses gcm. I came to know that if we uninstall the appfrom device and reinstalled most of the time the device gets new registration id.Then if we do not delete the old one from application server and update, The messages will be sending to both ids and in the response it will be showing canonical id is present.My question is, at this point message will be successfully send to that device or not?

Eran
  • 387,369
  • 54
  • 702
  • 768
user3256431
  • 167
  • 5
  • 15

2 Answers2

5

When you receive a canonical registration ID in the response from Google, the message was accepted by the GCM server and the GCM server would attempt to deliver it to the device. Whether it is actually sent to the device depends on whether the device is available (i.e. connected to the internet). So if your server sends a GCM message to both the old ID and the new ID, the device will probably get two messages.

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.

GCM provides a facility called "canonical registration IDs" to easily recover from these situations. A canonical registration ID is defined to be the ID of the last registration requested by your application. This is the ID that the server should use when sending messages to the device.

If later on you try to send a message using a different registration ID, GCM will process the request as usual, but it will include the canonical registration ID in the registration_id field of the response. Make sure to replace the registration ID stored in your server with this canonical ID, as eventually the ID you're using will stop working.

(Source)

You can overcome this problem by assigning a unique identifier to each instance of your application. If you store that identifier in the device's external storage, it won't be deleted when the app is uninstalled. Then you can recover it when the app is installed again. If you send this identifier to your server along with the registration ID, you can check if your server has an old registration ID for this identifier, and delete it.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • 2
    Eran, it seems like you're the person to ask anything gcm-related: I made a slight modification to your solution by storing the unique id (username) on the server along with the device's regid. Correct me if I'm wrong, but I should be fine because I'm only allowing one regid per user, and I can update accordingly. However this still assumes that the device will *always* have the most recent (and valid) regid. Is it safe to assume that? – user1164937 Aug 18 '14 at 21:46
  • 2nd question: Are there any other *edge cases* that I should be aware of, other than this and to make sure that I update the user's regid by the canonical id if there is one provided? Thanks in advance. – user1164937 Aug 18 '14 at 21:48
  • I forgot to mention that I'm using the exact same process as the google's example gcm app to register for the regid - when the sharedpreferences doesn't have one. (I apologize for the spam) – user1164937 Aug 18 '14 at 22:01
  • 2
    @user1164937 Your solution should work fine. If your app keeps the server updated each time it gets a new registration ID, and the server stores only the latest reg ID per user name, you should never even get a canonical reg ID in a response from GCM server. The canonical reg ID mechanism is required for cases in which the app is not behaving well. I don't know of any other edge cases. – Eran Aug 19 '14 at 04:38
0

@Eran We have two option either to remove older registration ids or update with Key and position of canonical id. I prefer to update... You can view working code of mine i have answered here Steps to Update Canonical Ids

Community
  • 1
  • 1
Raghu
  • 31
  • 7