简体   繁体   English

如何在卸载应用程序后从GCM取消注册

[英]How to unregister from GCM after uninstalling app

I have configured GCM in my application. 我在我的应用程序中配置了GCM。 I want to unregister device from GCM whenever user will uninstall application. 我想在用户卸载应用程序时从GCM取消注册设备。

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. 谢谢。

You cannot call unregister from GCM while uninstalling, because there is no method called while user uninstalls the application. 卸载时无法从GCM调用取消注册,因为在用户卸载应用程序时没有调用任何方法。

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. 当您发送推送通知时,GCM将检查用户是否有您的应用程序,如果用户已卸载该应用程序,GCM将记录相同的内容并通知您作为推送回复的一部分。

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. 请参阅:实现规范ID。 Reference: https://developers.google.com/cloud-messaging/http#request 参考: 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. 规范注册ID定义为应用程序请求的最后一次注册的ID。 This is the ID that the server should use when sending messages to the device. 这是服务器在向设备发送消息时应使用的ID。

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. 如果稍后您尝试使用不同的注册ID发送消息,GCM将照常处理请求,但它将在响应的registration_id字段中包含规范注册ID。 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. 确保使用此规范ID替换存储在服务器中的注册ID,因为最终您使用的ID将停止工作。

Reference: https://stuff.mit.edu/afs/sipb/project/android/docs/google/gcm/adv.html#canonical 参考: 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. 如果Canonical ID不为0,则表示您有重复注册。

Say for instance, you have 2 registrations in your database: 比如说,您的数据库中有2个注册:

registration_A registration_A

registration_B 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. 请注意,第一个具有“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. 只需在上面搜索“registration_id”并保存索引值即可。 This index value points to the old record in your database. 此索引值指向数据库中的旧记录。

In the above example, registration_A would be the old registration_id. 在上面的示例中,registration_A将是旧的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! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM