Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unregister from GCM after uninstalling app

I have configured GCM in my application. I want to unregister device from GCM whenever user will uninstall application.

I got the code as

Intent unregIntent = new Intent("com.google.android.c2dm.intent.UNREGISTER");
unregIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0));
startService(unregIntent);

but where we have to put this code..?

Thank You.

like image 915
Naresh J Avatar asked Sep 04 '12 10:09

Naresh J


2 Answers

You cannot call unregister from GCM while uninstalling, because there is no method called while user uninstalls the application.

when you send a push notification, GCM will check if the user has your application, if the user has uninstalled the application GCM will note the same and inform you as part of reply for the push.

like image 131
Yashwanth Kumar Avatar answered Nov 19 '22 12:11

Yashwanth Kumar


You have to check this on your server. You cannot do it from the application code since there is no way of knowing when the user is uninstalling the application.

See: Implement Canonical IDs. Reference: https://developers.google.com/cloud-messaging/http#request

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.

Reference: https://stuff.mit.edu/afs/sipb/project/android/docs/google/gcm/adv.html#canonical

If the Canonical ID is not 0, then you have a duplicate registration.

Say for instance, you have 2 registrations in your database:

registration_A

registration_B

When you send a push notification, your server will get respond with something that looks like this:

{"multicast_id":########,"success":1,"failure":0,"canonical_ids":1,"results":
[{"registration_id":"new_id_registration_id","message_id":"0:########"}]}
{"multicast_id":######### ,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:################"}]}

Store this data in an array. Notice that the first one has a "canonical_ids":1. This means there was a duplicate. So to know which record in your database is the old one. Just search for "registration_id" in the above and save the index value. This index value points to the old record in your database.

In the above example, registration_A would be the old registration_id.

Get all the records from your database. Then delete it based on the index value that you retrieved. OR you can update it. That depends on how you set up your database.

Good luck!

like image 1
Sandy D. Avatar answered Nov 19 '22 11:11

Sandy D.