How to automatically react to a Firebase Document Stream update even when Flutter app is in the background?
In the push notification, add the data as payload and send the notification. In the app, you should set up the part which handles, what happens when the notification is received. You can access the data
Here as you can see, the Firebase cloud messaging package has a method called onLaunch
and onResume
.
- OnLaunch handles the part when your app starts with a new lifecycle.
- onResume comes into play when your app was in the background and moved to the foreground.
You can access the payload information which you received from the notification through the message parameter. Now all you have to do is pass the necessary data to you calling screen as props.
refer: https://www.filledstacks.com/post/push-notifications-in-flutter-using-firebase/
Admin
Updated on November 27, 2022Comments
-
Admin over 1 year
I am building a videocalling flutter app (android + ios) that uses Firebase and Agora. In the app a StreamBuilder is setup which is open to a firestore stream -
callCollection.doc(uid).snapshots();
.When a call is placed while a user is in the app, the streambuilder in the app reacts as expected (opening up a pickup screen) but how do I achieve this when the app is in the background (not open or screen is off)? Even the app starting up automatically upon receiving an update is an acceptable reaction (the call's pickup screen would still appear).
I have Firebase Cloud Messaging enabled too for receiving push notifications, but I am not sure how to make use of them to trigger the app to open up automatically (without having the user tap the notification).
Thank you to anyone who spends time on helping me out or pointing me in the right direction!
-
Doug Stevenson over 3 yearsYou might want to start with the documentation for FCM to learn how to use it. You will need a backend to deliver the messages to your app. firebase.flutter.dev/docs/messaging/overview
-
Admin over 3 yearsHello Mr. Stevenson. I have looked at the FCM docs and in fact learnt using Firebase Cloud Functions from your Firecast videos. Currently I am not using a server to send messages but instead using POST requests from within the app. Notifications arrive correctly but want my app to be able to react with a bit more when a call arrives (Firestore document appears with the user's uid on it that the stream picks up) than just a notification.
-
scott lee almost 3 yearsHi @JawadAzizKhan, I'm facing a similar issue. Did you find a solution to it? Did you manage to get the app to react a bit more when a call arrives(i.e. vibrate or play a ringtone?)
-
-
Admin over 3 yearsThank you. I'll try it out. But isn't there a way to open up the app or something similar without needing the user to tap the notification?