0

I'm new to firebase realtime database, and have a basic question I cannot seem to find the answer to.

I need my Android app to keep track of changes in the database, so I understand I need to use addValueEventListener, with the onDataChange method. However will the method onDataChange, be called even if the app is destroyed? I need to be able to access the changes of the information in the database even if the app isn't running in the background, (for example the user force quits the app). This is because when the values reach a certain point, I want to show a pop up notification, so I need to able to read the values even when the app isn't running.

If the onDataChange is called even when the app is in the background, will this drain battery use since the phone is always listening for changes.

Sorry for the basic question, but I couldn't find the info.

Thanks!

RJB
  • 1,704
  • 23
  • 50
  • " need to be able to access the changes of the information in the database even the app isn't running in the background." You can't force an app to ALWAYS be alive in a phone. However, you can have a background service. – ivan Jun 20 '19 at 17:49
  • I slightly edited the quetions. I know you can't force app to be alive, but my question is when the app is dead, is the EventListener still listeneing, and will onDataChange be called? – RJB Jun 20 '19 at 17:53
  • If the app is dead its dead. the dead can't do things. if you have a background service and even that is dead well then, its dead. – ivan Jun 20 '19 at 17:54
  • Can you not just call your update with onPause? I am doing this with firestore and with minimal data. It might be a bad practice but you can send the update before it is destroyed – C. Skjerdal Jun 20 '19 at 18:00

1 Answers1

1

...when the app is dead, is the EventListener still listeneing, and will onDataChange be called?

Event listeners are only active while the context they run in is active. For listeners you attach in an activity, that means they're active while the app is running. Even then Android might kill the listeners (or more accurately: the socket they use for communicating with the server) if the user isn't actively using the app.

If you want the listener to stay active longer, you can indeed consider managing the listeners in a background service. But even there, Android might close the listener to preserve battery life. That is the one thing to always keep in mind: if your use-case interferes with the user's preference (and most users are likely strongly prefer longer battery life over any specific app feature), it's unlikely to continue working in the long run.

A better approach is to combine listeners with Firebase Cloud Messaging for sending messages. FCM messages are more likely (though still not guaranteed) to be delivered when the user is not actively using the app, and you can use them to run some code of your app when they arrive. You'll want to use FCM's data messages for this, which is how most app deliver background updates.

You can also use an FCM data message to just wake up your own code, then have that code attach a listener, and get its updates. This type of behavior is known as sending a tickle, since all the data message does is waking the application code up.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807