-1

Is there any method through which i can make my Firebase listeners active, even when the application is not running?

I need to add listeners at runtime, without using service.

Please help me out.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Vinay
  • 11
  • 6
  • What kind of listeners? Are you talking about push notifications? – elmorabea May 14 '18 at 12:53
  • I am not using push notification, simply fetch data from firebase-database, and i am using valueEventListeners. – Vinay May 14 '18 at 12:55
  • The best way to deliver messages to an inactive application is through Firebase Cloud Messaging, which helps reduce battery usage. See https://stackoverflow.com/questions/42606370/firebase-read-data-in-background-android, https://stackoverflow.com/questions/42210186/handling-keepsynced-while-on-background-on-android-and-with-fcm, and https://stackoverflow.com/questions/44826275/keep-data-from-the-firebase-realtime-database-always-in-sync – Frank van Puffelen May 14 '18 at 14:20

2 Answers2

0

In your case, to achieve this, simply don't remove the listener.

But remember, if you don't remove a listener when it's no longer needed, it will continue to receive changes that occur at the location where it's listening. So every time you add a listener without removing it, all the prior listeners will all still receive results. I can only imagine that repeating this many times will quickly consume the available network bandwidth on a device with a mobile data connection and obviously will cause slowness.

Here is how to remove the listener according to the life-cycle of your activity.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • isn't listener remove itself. when we remove the app from stack.? – Vinay May 14 '18 at 12:49
  • No, you need to remove it yourself as in my answer from that post. If you are using `addListenerForSingleValueEvent:`, then there is no need to remove the listener. – Alex Mamo May 14 '18 at 12:50
  • Then its not working, as i haven't remove listeners. – Vinay May 14 '18 at 12:55
  • If you are using `addValueEventListener()`, it means taht will keep listening to query or database reference it is attached to. Why are saying that is not working? – Alex Mamo May 14 '18 at 12:57
  • I meant, listener isn't working when my appplication isn't running. So i think, when we remove the app from stack, firebase listeners get removed too. – Vinay May 14 '18 at 12:59
  • No, Firebase listener are not removed when you close the application. You need to remove the listener yourself. If you close the application and you do some changes in your online database, the listeners will fire. Have you tried to make this test? – Alex Mamo May 14 '18 at 13:06
  • Is there everything alright? Have you solved the issue? – Alex Mamo May 16 '18 at 07:54
  • Not exactly, but i am trying some solution for now. – Vinay May 17 '18 at 16:12
  • Do you think that my answer helped you? – Alex Mamo May 17 '18 at 16:14
0

All the Firebase instances, whatever they are, are only in-memory instances inside your process, same goes for the listeners you add to them.

When your application's process is killed/stopped for whatever reason, all those listeners and instances will be garbage collected and will no longer be alive in memory, therefore will not trigger any updates.

Doesn't matter if you remove listeners or not, if the process hosting all those is gone.

If you want something to live in the background even if your app is not running. You should consider using a Service.

Or better explaining your use case would make it easier to help you.

There are not many -or any- use cases that will require realtime update ALL the time, every second of every hour of everyday.

elmorabea
  • 3,243
  • 1
  • 14
  • 20
  • my use case is like, restaurant app, where user can send request to the restaurant owner for booking, so now when user sen this request i show popup. Hope you are getting it. – Vinay May 14 '18 at 13:15
  • And there is no backend, you are only depending on Firebase syncing info for you? – elmorabea May 14 '18 at 13:58
  • 1
    Why dont you use push notifications for this purpose @vinay – Malavan May 14 '18 at 15:19