1

I'm trying to listen to Firebase's callback -

let ref = Database.database().reference()
ref.child(key).observe(.value) { (snapshot) in
            
   print("snapshot")
}

this method getting called every time I update some value inside the given key node.

Unfortunately, it's not getting called while the application is in the background. I know about background fetch but I don't want to fetch I want to wait for the callback, which will come, sometimes after 1 minute other times after 10 minutes (with background fetch I need to wait for the OS to decide that now it's a good time to make the fetch)

How can I stay alive in the background to listen to firebase callbacks?

EDITED:

I will explain more about my use case - I need to send the user a local notification every time the database node changes. but the listener is not called while the app is in the background so I can't invoke a local notification.

I know I can use push notifications but I don't want to make these changes to my database, I currently don't need users or device ID or anything like that, and I would like to keep it this way.

Edited

More about the use case - I have 4 players in a long turn-based game. While player 1 plays, player 4 will probably not stay in the app because it can take some time (from 1 - 30 minutes). I want to notify player 4 when it is his turn. Each player sets a turn value every time he finishes his turn and the other players are listening to changes in that value.

On android, we just keep listening to these changes all the time even in the background, and when turn changes, the listeners get the callback, and from that callback, we notify the user "it's your turn" via local notification. I would like to do the same on iOS.

ozd
  • 1,194
  • 10
  • 34
  • This question and similar questions have been asked many times here on SO [here](https://stackoverflow.com/questions/37899712/fcm-background-notifications-not-working-in-ios) and [this one](https://stackoverflow.com/questions/36592137/ios-firebase-background-fetch) and [another](https://stackoverflow.com/questions/41683822/firebase-notification-doesnt-work-in-background) and there are a number of solutions. It would be helpful to know what exactly you expect to happen when your app is in the background, and what's different about your use case than in the other questions. – Jay Sep 05 '20 at 13:51
  • These answers talk about notifications from firebase OR background fetch which - 1. "I don't want to fetch I want to wait for the callback" 2. "with background fetch I need to wait for the OS to decide that now it's a good time to make the fetch" – ozd Sep 05 '20 at 20:35
  • Right. So we would need to know more specifically what your attempting to do and what the expected result is so we can understand your use case and how it's different. Please update the question and we'll take a look! – Jay Sep 05 '20 at 22:48
  • It's still a bit unclear and you've not told us what exactly the app will be doing while it's in the background or what it will do with any data it receives. Your edit says *but the listener is not called while the app is in the background* which is correct and that's how Firebase (or really any app) operates. If you read through the links I provided above they all pretty much state the same thing and some of the answers provide alternates. See [this answer](https://stackoverflow.com/questions/39011182/firebase-database-transaction-when-app-is-in-background-ios) as well. – Jay Sep 06 '20 at 12:37
  • This is false. First link - FCM background notifications not working in iOS - FCM... Second link - Fetch calls NOT realtime listener. Third link - 1 answer with "-1" rating and ALSO talks about getting a notification in the background NOT listener. Forth link - is related but I thought somebody may have a more updated answer (this is from 2016). – ozd Sep 08 '20 at 09:02
  • I need a listener callbacks NOT a remote notification. My use case is to send notifications to the user (no need to update the app) I just want to inform the user that this is his turn to play so he needs to get in the app if he wants. so when a player updates that he finished his turn I want the other player to get the callback to the listener and send the user a local notification from that code block. Is this possible? – ozd Sep 08 '20 at 09:02
  • It's not clear why you need a listener when the app is in the background because it's not processing any data; when in the background there are no active listeners so the only option is notifications. That being said, the notification can bring the app forward or trigger a number of other events so that's the solution. – Jay Sep 08 '20 at 17:33
  • Ok... I guess I just need to accept it :), thank you. are you a Firebase employee? I just want to know if this is the official answer from Firebase. – ozd Sep 10 '20 at 11:06
  • I am not an employee but am pretty well versed in Firebase. That being said, I didn't actually provide an answer; just speaking generally about background functionality. It's not clear what's expected to happen in the background so an answer is not possible. Apple limits what can be done in the background to very specific cases; playing music, getting location updates or a background data fetch. While the last case sounds like it may fit, the OS determines when that fetch happens. From Apple **a background app must do as little work as possible and preferably nothing, because it is offscreen** – Jay Sep 10 '20 at 19:15
  • Check out Apple Documentation on [Managing Your App's Life Cycle](https://developer.apple.com/documentation/uikit/app_and_environment/managing_your_app_s_life_cycle) and this [Q&A](https://stackoverflow.com/questions/39011182/firebase-database-transaction-when-app-is-in-background-ios). And probably [this question](https://stackoverflow.com/questions/40337765/just-how-persistent-are-firebase-database-observers) which was answered by a Firebaser @frankvanpuffelen (official employee) – Jay Sep 10 '20 at 19:24
  • Unfortunately, I think you did provide an answer which is - "Firebase listener is not using or included in any special background mode, and will listen, while the app is in the background, for a very short time if any before the app will turn idle by the OS, at this point callbacks are no longer available. You can use background fetch for some scenarios". in my case backgrounds fetch is not an option. Thanks @Jay – ozd Sep 11 '20 at 20:12
  • Not sure where that quote came from (wasn't me) but that wasn't an answer I provided. Not saying it's incorrect but not sure how that applies since we still don't know what the use case is for working with data when the app is not active - I mean, what are you expecting to happen to data when the app isn't really running or processing since it's in the background? Maybe you can update the question to help clarify? – Jay Sep 12 '20 at 11:24
  • The question makes more sense but ouch, talk about burning up the battery. I'm not an android guy but that doesn't sound like it fits into how to handle [Background Execution](https://developer.android.com/about/versions/oreo/background) - however, I don't know your implementation. Either way, while the app is in the background you don't really care about the specific changes, only that the data did change. [Firebase Cloud Functions](https://firebase.google.com/docs/functions) and [Notifications](https://firebase.google.com/docs/cloud-messaging/ios/receive) are perfect for this use case. – Jay Sep 13 '20 at 14:14
  • OK Thanks for all the help, as long as I have your attention I will be rude and ask 1 more question - is there a way to upload cloud functions without changing to pay as you go plan? It is not uploading unless I'm using my credit card on a google cloud which is insane, if I have a problem with my code (loop) I could be owing them millions. – ozd Sep 14 '20 at 02:10
  • I don't know what *upload cloud functions* means. Cloud functions are stored on the Firebase server and that code executes there. There is no billing for running a cloud function, other than the cost of the data it moves around. – Jay Sep 14 '20 at 17:08

0 Answers0