GCM as in Google cloud messaging gives us a way to implement/use push notifications in a easier way by using registration tokens. But these tokens are periodically or due to some reason can refresh/ regenrated . So the older one are not for use so we must manage them.
Before moving forward, you must have an implementation of GCM in your project. Each time the app unistalls and reinstalled a new token is created. Also to protect the client app and app server from potential malicious re-use of registration tokens, you should periodically initiate token refresh from the server. This causes the old token to be marked for deletion by GCM.Also when there is some error in clients app because of which multiple tokens are generated
All this can cause many problems like extra memory allocation at your server database, duplicate messages at clents app, etc. To solve the problem there are two methods
Method 1: Canonical Ids
Implementing canonical IDs can help you more easily recover from these situations. 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.
Now just delete those 2 registration ids.
Method 2: NotRegistered error message
When the old id expires or is changed by the new id then the old registration id is market for deletion by GCM and when your server sends a notification to that registration id , the server sends NotRegistered error message.So for every value you recieve NotRegistered error message, delete that token from your database.
Thats all, you can use any method that suits you best.
Be the first to comment.