Handle GCM Canonical Id with Amazon SNS

845 views Asked by At

I have questions. How can I handle situation when GCM return canonical id. I found few same questions on StackOverflow, but there is no solution. Amazon SNS Documentation about token management says that SNS handle this situation automatically. When GCM return canonical id amazon SNS update old tokens with new ones. But how can I know about that? I have my own database with all registered token and endpoints. I don't know if amazon was update token or not and send notification to all of them. As result lots of duplicate messages on device.

P.S. Server have to support multiple devices for one account.

2

There are 2 answers

0
varna On BEST ANSWER

Amazon SNS will handle token management only if your app is registered with them. Since you have your own database and endpoints, you would have to manage it yourself.

After pushing the message from the server, you would recieve the canonical id in the response.

As per Google's docs,

A canonical registration ID is the registration token of the last registration requested by the client app .This is the ID that the server should use when sending messages to the device.

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

It is good practice to update your registration id with the canonical id returned after the push.

Also if your device is receiving duplicate notifications these could help:-

  • It could be due to a bug in the client app triggering multiple registrations for the same device. So make sure the same device is not getting registered many times.

  • Also, if you uninstall the app from device and you try to send a push to that device, you would get a NotRegistered error from GCM server. Then your server should delete the registration token and not use that registration token again to push messages.

P.S. Even if one user has multiple devices, your db mappings should be such that :- each device of the user will have a different registration token and each of the tokens will be mapped to a single user. Since the tokens are different, receiving the same message pushed on all devices of the user would not be a problem.

Hope this helps! Cheers!

0
George Lin On

Please take a look at http://mobile.awsblog.com/post/Tx223MJB0XKV9RU/Mobile-token-management-with-Amazon-SNS for information on how to manage the mobile tokens.