3

I'm very new to Firestore and trying to understand how the real-time updates work. I'm using something like this now to get the updates from Firestore:

db
  .collection(Collections.session)
  .whereField("participants", arrayContains:userID)
  .addSnapshotListener { querySnapshot, error in

I noticed that the listener block is not getting fired when the app is in the background, but only when it's brought back to foreground.

Is there a way to get the update when the app is running in the background too? Maybe somehow send a push notification or something?

Any kind of help is highly appreciated.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Eugene Gordin
  • 4,047
  • 3
  • 47
  • 80

2 Answers2

6

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.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
0

Push notification is not a reliable way to do this unfortunately. that backgrounded apps may be killed by the OS, if needed, but not always. The source of termination can be the OS or the user. If the user terminates your app, there is no way to receive a push notification.

Eagle
  • 18
  • 3