Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android GCM and multiple tokens

I register in GCM with GoogleCloudMessaging.getInstance(context); and save received token on device. Then send it to server and it's associated with user account. If I uninstall my app without logging out and install again and log in with another user, I receive new token and send it to server. And when pushes being send to first user I see them when I logged in with second user.

Why does GCM sends me different tokens and how can I handle this?

like image 393
earsonheart Avatar asked Apr 25 '14 12:04

earsonheart


1 Answers

Welcome to the marvelous world of duplicate messages from Google Cloud Messaging. When this happens, the GCM engine enables the Canonical IDs to solve it. This might happen because you registered with several IDs for the same device, or because the GCM server didn't get the unregister() call when the app was uninstalled. Using canonical IDs will set your ID to be the last registration you've made.

As per the GCM reference about this:

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.

More info here.

Also there is a practical case on how to procceed, it might be helpful:

  • Canonical Registration ID and message ID format
like image 154
nKn Avatar answered Sep 28 '22 07:09

nKn