简体   繁体   English

是否可以检测到 Android 应用程序卸载?

[英]Is it possible to detect Android app uninstall?

My app is using Google's C2DM (push notification) to notify users about new activity from friends.我的应用程序正在使用 Google 的 C2DM(推送通知)来通知用户来自朋友的新活动。 Once they install the app I register the device with C2DM servers and store user's phone number.一旦他们安装了应用程序,我就会在 C2DM 服务器上注册设备并存储用户的电话号码。 So I know that the user is using my app and I can send him/her the push notifications.所以我知道用户正在使用我的应用程序,我可以向他/她发送推送通知。 But what happens if users uninstalls my app, is there a way to catch it in my app?但是如果用户卸载我的应用程序会发生什么,有没有办法在我的应用程序中捕获它? Or the only way is to catch an error on my server when I send a C2DM and it's unreachable, then mark a user as inactive?或者唯一的方法是在我发送 C2DM 并且无法访问时在我的服务器上捕获错误,然后将用户标记为非活动?

I would love to notify users when their friends are using an app and when they no longer do.我很乐意在他们的朋友使用应用程序以及不再使用应用程序时通知用户。

What's is the best solution for this scenario?这种情况的最佳解决方案是什么?

The GCM documentation explains this situation here: GCM 文档在这里解释了这种情况:

https://developers.google.com/cloud-messaging/registration#how-uninstalled-client-app-unregistration-works https://developers.google.com/cloud-messaging/registration#how-uninstalled-client-app-unregistration-works

"An application can be automatically unregistered after it is uninstalled from the device. However, this process does not happens right away, as Android does not provide an uninstall callback." “应用程序从设备上卸载后可以自动取消注册。但是,此过程不会立即发生,因为 Android 不提供卸载回调。”

Basically when GCM tries to send the next push notification, the device will tell GCM the receiving application was uninstalled.基本上,当 GCM 尝试发送下一个推送通知时,设备会告诉 GCM 接收应用程序已卸载。

As for notifying friends that their friends aren't using the app any more, GCM will send a NotRegistered error to your notification server when this failure occurs;至于通知朋友他们的朋友不再使用该应用程序,当此故障发生时,GCM 会向您的通知服务器发送NotRegistered错误; it won't be immediate, but could you use that?它不会立即生效,但你可以使用它吗?

Unfortunately the ACTION_PACKAGE_REMOVED intent will be sent out to all receivers except for your own.不幸的是 ACTION_PACKAGE_REMOVED 意图将被发送给除您自己之外的所有接收者。 This is confirmed here .这一点在这里得到证实。

Some questions for your C2DM plan, since I'm not very familiar with it.关于您的 C2DM 计划的一些问题,因为我对它不是很熟悉。 If the user just leaves their device off for a long period of time, will that trigger the error condition you use?如果用户只是长时间关闭他们的设备,这会触发您使用的错误条件吗? How does C2DM actually report an "unreachable" device? C2DM 如何实际报告“无法访问”的设备? Is that a condition that only occurs when it attempts to send the push notification and fails or is it when it somehow determines it reaches the device but fails to be handled properly?这是仅在尝试发送推送通知但失败时发生的情况,还是在以某种方式确定它到达设备但未能正确处理时发生的情况? Obviously in the second scenario your plan would work, but I can see some "false positives" occurring otherwise.显然,在第二种情况下,您的计划会奏效,但我可以看到一些“误报”发生在其他情况下。

Older SO question for reference: android not receiving Intent ACTION_PACKAGE_REMOVED in the removed package较早的 SO 问题供参考: android not received Intent ACTION_PACKAGE_REMOVED in the removed package

Yes, but it is quite hacky.是的,但它非常hacky。 The method is based on the fact that the first thing android does when uninstalling your app is deleting your data file.该方法基于android在卸载您的应用程序时所做的第一件事是删除您的数据文件的事实。 So you could use a file watcher to detect the deletion.因此,您可以使用文件观察程序来检测删除。 Also you need to write this in native code.您还需要用本机代码编写它。 If you write your code in java, your app will be uninstalled before it could execute any code.如果您在 java 中编写代码,您的应用将在执行任何代码之前被卸载。 please see this demo: https://github.com/sevenler/Uninstall_Statics请看这个演示: https://github.com/sevenler/Uninstall_Statics

Google C2DM service is working in passive mode when it comes to detecting uninstalled applications.在检测已卸载的应用程序时,Google C2DM 服务以被动模式工作。

First push notification after uninstalling your application (without unregistering from C2DM.,.) will NOT return any error in response.卸载您的应用程序后的第一次推送通知(无需从 C2DM 取消注册。,。)将不会返回任何错误作为响应。 However, the second push notification will return an "invalid registration" or "not registered" error codes where you can realize the application was uninstalled.但是,第二次推送通知将返回“无效注册”或“未注册”错误代码,您可以在其中意识到应用程序已被卸载。

The reason is that C2DM servers return the response code immediately and only then tries to push the client.原因是 C2DM 服务器立即返回响应代码,然后才尝试推送客户端。 When client respond that an application was uninstalled, it is deleted from C2DM servers.当客户端响应应用程序已卸载时,它会从 C2DM 服务器中删除。 Next push attempt will return an error code immediately.下一次推送尝试将立即返回错误代码。

I have some points to tell you,我有几点要告诉你,

  1. Android community recommends you to use GCM instead of C2DM as it's no longer available. Android 社区建议您使用 GCM 而不是 C2DM,因为它不再可用。
  2. In android there is no way for applications to get itself notified that app is getting uninstalled.在 android 中,应用程序无法收到应用程序正在卸载的通知。
  3. in GCM if you want to stop sending messages to uninstalled apps you can refer this在 GCM 中,如果您想停止向已卸载的应用程序发送消息,您可以参考这个

When you send messages to GCM from your server you will get response string.In that if you are getting error as "NotRegistered, you should remove the registration ID from your server database because the application was uninstalled from the device or it does not have a broadcast receiver configured to receive com.google.android.c2dm.intent.RECEIVE intents."当您从服务器向 GCM 发送消息时,您将收到响应字符串。如果您收到“未注册”错误,您应该从服务器数据库中删除注册 ID,因为该应用程序已从设备上卸载或者它没有广播接收器配置为接收 com.google.android.c2dm.intent.RECEIVE 意图。”

I know only one way with server response 200 with "NotRegistered" message in body.我只知道服务器响应 200 中带有“NotRegistered”消息的一种方式。

NotRegistered — The registration_id is no longer valid, for example user has uninstalled the application or turned off notifications. NotRegistered — registration_id 不再有效,例如用户已卸载应用程序或关闭通知。 Sender should stop sending messages to this device.发件人应停止向此设备发送消息。

Look into this GCM doc: GCM Unregistration查看此 GCM 文档: GCM 注销

You should never unregister your app.您永远不应该取消注册您的应用程序。 This is taken care from server side.这是从服务器端处理的。

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

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