Can You Listen to Firestore Updates When iOS App Is in The Background

Can you listen to Firestore updates when iOS app is in the background?

Is there a way to get the update when the app is running in the background too?

Since backgrounded apps are eventually killed by the OS, you don't have a way to run a listener reliably when the app is not actively being used by the user.

You are correct in that the only way to (reliably) notify your app of some change in your backend is to send a push notification.

A very common approach is to use Cloud Functions to write a Firestore trigger that gets invoked when a document of interest is created, updated, or deleted. You can use this to write backend code that uses Firebase Cloud Messaging and the Firebase Admin SDK to send a notification to your app with a payload that tells it to respond to that change.

Firestore listener delayed when coming from background in iOS

I was able to fix this by detaching the Firestore listener when the app goes to the background Detach a Listener

I was able to achieve this by using Expo's AppState

Firestore listener killed when app goes in background for a long time

On most operating system this is done automatically by the system itself, in order to preserve battery life. Whether it is possible to detect this situation, and how, depends on the OS.

In most cases it is better to depend on another mechanism to deliver updates to an app that is not actively being used, such as Firebase Cloud Messaging - which you can call from for example Cloud Functions to deliver messages to backgrounded apps. For an example of this, see the Firebase documentation on notifying users when something interesting happens.

Firebase listener when app is in the background

Devices have their own way of managing background network calls so handling background push notifications like this will not work on real devices. The observers are removed from memory shortly after the app goes into the background. I was able to solve this by creating a Node.js server which observes the notifications table and handles push notifications using the Firebase-Messenger framework. It was actually pretty easy. I used this blog post as my guide:
Sending notifications between Android devices with Firebase Database and Cloud Messaging

This method will work however in the simulator (but not on real devices) so long as you pass the object containing the observer (the app delegate in this case) to background thread call like this:

func applicationWillResignActive(_ application: UIApplication) {
NotificationCenter.default.addObserver(self,
selector: #selector(self.observeNotificationsChildAddedBackground),
name: notificationBackgroundProcessName,
object: self)
}

listen to Firestore's onSnapshot while app is not running

Most mobile operating systems severely limit what the app can do while it is backgrounded. Keeping an active connection open to a server is one of those things that has never been allowed on iOS, and has gotten more restrictive with recent Android versions.

The better approach is to run the code that detects a relevant change on a server, and then use Firebase Cloud Messaging (or APNS directly if you're only targeting iOS) to send a notification to the user. When they they tap on that notification, your app is started and you can load the data from Firestore.

If you don't want to run your own server, Cloud Functions is a common alternative for this, as it has built in triggers that respond to changes in Firestore. For an example of this exact scenario, see the documentation on notify users when something interesting happens.

Listening for changes in Firestore database in and out of the App - Swift

Should you implement a FCM notification for the second scenario, the notification will be fired to your app no matter if it is on background or foreground, so you can use this approach for both situations actually.

You can use this solution to not fire a notification (on the frontend) and in case you don't want to fire that if the app is in foreground you can just keep the completionHandler() blank and that's it.

should SnapshotListeners be recalled each time that an app is restored from the background or returned to active

As Firestore documentation suggests, when you're no longer interested in listening to particular data then you should stop listening. If your app isn't configured to perform in the background, and you don't want it to, then you shouldn't be listening to data in the background. The most obvious reason for this is economy of engineering (we should always strive for maximum efficiency and if getting data updates in the background isn't necessary then it shouldn't be done). However, more practically, consider that an app is more likely to be killed by the OS if it's performing work in the background (versus an app that isn't) when other apps strain resources and that you're ultimately wasting money by paying for reads that will never be seen by anyone.



Related Topics



Leave a reply



Submit