I have a Flutter app integrated with a Firebase Realtime Database.
I want a notification (actually an alarm kind of thing if possible), when an item (lets call it alarm) in the database is set to "true". I always see firebase_messaging plugin for notifications, but I am not sure if I’m supposed to use this plugin despite that my app doesn’t have anything to do with messaging.
I am totally new to both Flutter and Firebase, can you tell me how to listen to the database even if the app is not running?
By the way, I am currently building the app for only Android, but I want to build it for IOS too in the future.
When the user is not actively using the app there is no reliable way to continue to listen to changes in the Firebase Realtime Database. To notify the user of changes to the database in that situation, you’ll need to listen for those change on an environment that is always on, and then send a message to the user through Firebase Cloud Messaging.
Developers can use Cloud Functions to keep users engaged and up to date with relevant information about an app. Consider, for example, an app that allows users to follow one another’s activities in the app. Each time a user adds themselves as a follower of another user, a write occurs in the Realtime Database. Then this write event could trigger a function to create Firebase Cloud Messaging (FCM) notifications to let the appropriate users know that they have gained new followers.
- The function triggers on writes to the Realtime Database path where followers are stored.
- The function composes a message to send via FCM.
- FCM sends the notification message to the user’s device.
To review working code, see Send FCM notifications.
Answered By – Frank van Puffelen
Answer Checked By – David Marino (FlutterFixes Volunteer)