Is it possible to detect Android app uninstall?

2018-12-31 18:05发布

My app is using Google's C2DM (push notification) to notify users about new activity from friends. Once they install the app i register the device with C2DM servers and store user's phone number. 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?

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?

7条回答
初与友歌
2楼-- · 2018-12-31 18:29

Unfortunately the ACTION_PACKAGE_REMOVED intent will be sent out to all receivers except for your own. This is confirmed here.

Some questions for your C2DM plan, since I'm not very familiar with it. 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? 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

查看更多
牵手、夕阳
3楼-- · 2018-12-31 18:34

I know only one way with server response 200 with "NotRegistered" message in body.

NotRegistered — The registration_id is no longer valid, for example user has uninstalled the application or turned off notifications. Sender should stop sending messages to this device.

查看更多
流年柔荑漫光年
4楼-- · 2018-12-31 18:37

Google C2DM service is working in passive mode when it comes to detecting uninstalled applications.

First push notification after uninstalling your application (without unregistering from C2DM!!!) will NOT return any error in response. 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. When client respond that an application was uninstalled, it is deleted from C2DM servers. Next push attempt will return an error code immediately.

查看更多
永恒的永恒
5楼-- · 2018-12-31 18:42

Yes, but it is quite hacky. The method is based on the fact that the first thing android does when uninstalling your app is deleting your data file. 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. please see this demo : https://github.com/sevenler/Uninstall_Statics

查看更多
墨雨无痕
6楼-- · 2018-12-31 18:43

The GCM documentation explains this situation here:

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."

Basically when GCM tries to send the next push notification, the device will tell GCM the receiving application was uninstalled.

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; it won't be immediate, but could you use that?

查看更多
初与友歌
7楼-- · 2018-12-31 18:46

Look into this GCM doc: GCM Unregistration

You should never unregister your app. This is taken care from server side.

查看更多
登录 后发表回答