4

I have successfully implemented a client/server Android GCM application. My app server holds a database of device/registration_id pairs. Messages are sent to the devices via web accessible PHP pages. I am trying to write the error handling on the server side, specifically the case where the response contains a canonical_id. The developer docs are quite clear about what needs to be done and why such a response might be produced:

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.

So within my Android client app I have a development only button which will register the device with GCMRegistrar.register() a second time without bothering to unregister it first. I had assumed that this would get me a registration_id different from the first time I registered it, and that sending a message to the device from the web (maybe using the old id) would give me a response with a canonical_id.

However the second registration returns exactly the same reg_id as the first time.

Thus sending a message via the PHP on my server won't show a canonical_id in the response. I really need to test the situation where a canonical_id is returned, indicating that I need to update my database as my PHP skills are not the finest!

So any ideas as to how I could provoke such a response would be gratefully received

Update: I just tried uninstalling the app from the phone, then reinstalling it. I still get the same reg_id.

Eran
  • 387,369
  • 54
  • 702
  • 768
NickT
  • 23,844
  • 11
  • 78
  • 121

2 Answers2

3

I answered that question - here. You can try following the steps I used to get a canonical registration Id. It did involve uninstalling the App, but perhaps you didn't do all the steps I describe there.

Community
  • 1
  • 1
Eran
  • 387,369
  • 54
  • 702
  • 768
  • Yes, that works perfectly. You must have had to try a lot of different, seemingly nonsensical, sequences to finally get a result. Well done. – NickT Nov 01 '12 at 11:27
  • Actually I didn't even try to get the canonical reg id. I just tried to get all the possible errors, which is why I uninstalled the app (to get the NotRegistered error). After re-installing the app I was curious to know what would happen if I used the old reg id again. Glad I could help. – Eran Nov 01 '12 at 17:24
0

My understanding is that canonical id that needs updating will only come from the server. This will only occur if GCM servers detect that they have two ids for a single device.

This might only happen if there is a race condition of some sort, or other bug that is difficult to reproduce. I understand your yearning to test this scenario, but I've been using GCM since it came out of beta and have never seen a canonical id replacement come through.

Shellum
  • 3,159
  • 2
  • 21
  • 26
  • My understanding too. However they should provide a test scenario, so that if you put a special extra on the request say, then you will get a canonical_id. How else can they expect developers to write decent, tested apps? – NickT Oct 31 '12 at 14:38
  • Or at least give some more concrete idea as to how the duplication could occur. I agree. – Shellum Oct 31 '12 at 14:42