Flutter Notifications with Firebase Realtime Database


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.

One environment that is always in is Cloud Functions, which is also part of Firebase, and allows you to run small snippets of JavaScript code on Google’s servers in response to things that happen in your Firebase project. The documentation of Cloud Functions for Firebase as example of how to notify the user when something interesting happens in the database:

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.

Diagram showing the app flow described below

  1. The function triggers on writes to the Realtime Database path where followers are stored.
  2. The function composes a message to send via FCM.
  3. 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)

Leave a Reply

Your email address will not be published. Required fields are marked *