Firebase Listener When App Is in the Background

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)
}

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.

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

Alert with closed app or in background - Flutter

What was unsuccessful about using push notifications? I don't think think it will work without a backend pushing the notification, possibly if the app is running in the background. A simple solution would be to use a Google Cloud Function which waits for the changes in the database, then send a Google Cloud Message (push notification) through that function.

See this article for details on how to do this:

Send Firebase Cloud Messaging(FCM) or Push notification to a topic using cloud function when real-time database value changes.

Using a listener after an android application is closed

Using a listener after an android application is closed

You can use a listener after an android application is closed, by not removing it. Once you are using a listener, you also need to remove it according to the life-cycle of your activity. But this will work only for a shot period of time because Android will stop your service if the app is not in the foreground. It does this to save resources when the app isn't being used. It also might stop your app from doing any networking, or even kill the app process completely. There's nothing you can do to prevent this, other than making it a foreground service, as you already mentioned.

A foreground service is probably not the best thing to do for your case, nor is it the best thing for your users. Read more about limitations on background services.

My recommendation is to use Firebase Cloud Messaging to notify your app when something has changed that it might be interested in. So your users will recieve notification even if they will keep their app closed.



Related Topics



Leave a reply



Submit