1

I have an android application where want to update my remote database when the application is killed. Now I use onTaskRemoved() method of one of my Service where I handle when the application is removed from the current applications list and send Intent to BroadcastReceiver where call a request to my remote database (want to wait the response from the back-end if the write operation is success). This approach not works all the time.

The main idea is to know which users are in their applications and which killed it.

MrVasilev
  • 1,503
  • 2
  • 17
  • 34

2 Answers2

1

You can use onDisconnect() to update the database when a device is no longer connected. Usage would look like:

databaseReference.onDisconnect().setValue(true);

You set this while your app is running, the remote database doesn't immediately write it, but when your app disconnects, then Firebase will update that value to the database.

More about onDisconnect() as well as an example for showing when a user goes offline are found in the documentation here.

Lewis McGeary
  • 7,692
  • 2
  • 40
  • 46
  • Thank you Lewis. Now I am testing onDisconnect() and it works good. – MrVasilev Apr 12 '17 at 10:32
  • Lewis, when I put my application in background for 5-6 min and then kill it (remove from the current application list), the onDisconnect().setValue(true); not works – MrVasilev Apr 12 '17 at 10:40
0

Firebase hasn't provided support for such thing. So you need to work something out. Firebase updates data only when the app is in background not when it is killed.

Or try this this might help

Keep app running in background on Android all the time

jack
  • 7
  • 5