How to Add Local Notifications in Flutter?
Last Updated :
24 Apr, 2025
A push notification is a short message that appears as a pop-up on your desktop browser, mobile home screen, or in your device notification center from a mobile app. For receiving and showcasing push notifications in flutter using local_notifications. There are 3 conditions when we receive notifications
- When the app is closed.
- When the app is opened and the user is using it.
- When the app is not opened not it is completely closed. It is running in the background.
Here we will handle all the notifications.
Step By Step Implementation
Step 1: Create a Flutter project
flutter create .
Step 2: Add local notification package
// Add in dependencies in pubspec.yaml file
flutter_local_notifications:
Step 3: Create 1 class for adding all local notifications functions and add an Instance of local notification in it
Dart
class LocalNotificationService {
// Instance of Flutternotification plugin
static final FlutterLocalNotificationsPlugin _notificationsPlugin =
FlutterLocalNotificationsPlugin();
}
Step 4: Add initialize function in this class
Dart
static void initialize() {
// Initialization setting for android
const InitializationSettings initializationSettingsAndroid =
InitializationSettings(
android: AndroidInitializationSettings("@drawable/ic_launcher"));
_notificationsPlugin.initialize(
initializationSettingsAndroid,
// to handle event when we receive notification
onDidReceiveNotificationResponse: (details) {
if (details.input != null) {}
},
);
}
Step 5: Call this function in the Widget you are putting in the runApp function like this
Dart
Future main() async {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
// Initialise localnotification
LocalNotificationService.initialize();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.green,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
Step 6: For handling all these conditions we have to add firebase messaging and for this add firebase to your flutter project
To add firebase to your project read this article How to Add Firebase to Flutter App.
Dart
Step 7: To handle background notifications add this to the main function
Dart
Future main() async {
FirebaseMessaging.onBackgroundMessage(backgroundHandler);
// To handle background message
runApp(const MyApp());
}
// background handler
Future backgroundHandler(RemoteMessage msg) async {}
Step 8: Now to handle the other 2 conditions add this in the init function below initialization of local notification
Dart
@override
void initState() {
super.initState();
LocalNotificationService.initialize();
// To initialise the sg
FirebaseMessaging.instance.getInitialMessage().then((message) {
});
// To initialise when app is not terminated
FirebaseMessaging.onMessage.listen((message) {
if (message.notification != null) {
LocalNotificationService.display(message);
}
});
// To handle when app is open in
// user divide and heshe is using it
FirebaseMessaging.onMessageOpenedApp.listen((message) {
print("on message opened app");
});
}
Step 9: To display notifications you have to add this function in the local notification
Dart
static Future<void> display(RemoteMessage message) async {
// To display the notification in device
try {
print(message.notification!.android!.sound);
final id = DateTime.now().millisecondsSinceEpoch ~/ 1000;
NotificationDetails notificationDetails = NotificationDetails(
android: AndroidNotificationDetails(
message.notification!.android!.sound ?? "Channel Id",
message.notification!.android!.sound ?? "Main Channel",
groupKey: "gfg",
color: Colors.green,
importance: Importance.max,
sound: RawResourceAndroidNotificationSound(
message.notification!.android!.sound ?? "gfg"),
// different sound for
// different notification
playSound: true,
priority: Priority.high),
);
await _notificationsPlugin.show(id, message.notification?.title,
message.notification?.body, notificationDetails,
payload: message.data['route']);
} catch (e) {
debugPrint(e.toString());
}
Also, add these settings to the android folder
1. Create a file named colors.xml in android/app/src/main/res/values and add these codes to this file
XML
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<color name="green">#00FF00</color>
</resources>
2. Add this code in android/app/src/main/AndroidManifest.xml
XML
<meta-data android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/ic_launcher">
<meta-data android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/green">
Note:
If you want to receive notifications you need 1 token which is unique for every phone and may expire in some time. You need to store this token in your server if you want to send notifications to individual people To get the token you can use this function of firebase messaging
Dart
// in some device or platform specially in web
// it may happen gettoken is not supported then
// you can put these condition to so that
// you can not get any error in production
var notifyToken = FirebaseMessaging.instance.isSupported()
? await FirebaseMessaging.instance
.getToken()
: "notifyToken";
Now you can receive notifications on your android phone. For receiving notifications on the web you have to do the following settings. Create this file in the web folder and named as firebase-messaging-sw.js
JavaScript
importScripts('https://www.gstatic.com/firebasejs/8.4.1/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/8.4.1/firebase-messaging.js');
/*Update with yours config*/
const firebaseConfig = {
apiKey: "YOur Key-EW_U6NZU-A",
authDomain: "flutter-notification-test-gfg.firebaseapp.com",
projectId: "flutter-notification-tes-fec9d",
storageBucket: "flutter-dsd-tes-fec9d.gfg.com",
messagingSenderId: "263307381024",
appId: "1:263307381024:web:dsds",
measurementId: "G-dsada"
};
firebase.initializeApp(firebaseConfig);
const messaging = firebase.messaging();
/*messaging.onMessage((payload) => {
console.log('Message received. ', payload);*/
messaging.onBackgroundMessage(function(payload) {
console.log('Received background message ', payload);
const notificationTitle = payload.notification.title;
const notificationOptions = {
body: payload.notification.body,
};
self.registration.showNotification(notificationTitle,
notificationOptions);
});
3. Do the following changes in index.html
HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>flutter_notification_test</title>
<script src="https://www.gstatic.com/firebasejs/8.4.1/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.4.1/firebase-messaging.js"></script>
</head>
<body>
<script>
<!--Update with yours config-->
const firebaseConfig = {
apiKey: "your key-EW_U6NZU-A",
authDomain: "flutter-notification-temp-fec9d.gfg.com",
projectId: "flutter-notification-tes-fec9d",
storageBucket: "flutter-dasdadas-tes-fec9d.appspot.com",
messagingSenderId: "dasdsa",
appId: "1:263307381024:web:dsadads",
measurementId: "G-dsadsa"
};
firebase.initializeApp(firebaseConfig);
</script>
<script src="main.dart.js" type="application/javascript"></script>
<script>
if ("serviceWorker" in navigator) {
window.addEventListener("load", function () {
navigator.serviceWorker.register("/firebase-messaging-sw.js");
});
}
</script>
</body>
</html>
4. Add this function in location_notification_service.dart file in the lib folder
Dart
void messageListener(BuildContext context) {
// Either you can pass buildcontext or you
// can take a context from navigator key
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
print('Got a message in the foreground!');
print('Message data: ${message.data}');
if (message.notification != null) {
print(
'Message also contained a notification: so it will pop up ${message.notification.body}');
showDialog(
context: context,
// context: navigatorKey!.currentContext!,
builder: ((BuildContext context) {
return DynamicDialog(
title: message.notification.title,
body: message.notification.body);
}));
}
});
}
For dynamic dialog either add this code below localnotificationservice class file or create a new file
Dart
import 'package:flutter/material.dart';
class DynamicDialog extends StatefulWidget {
final title;
final body;
const DynamicDialog({this.title, this.body});
@override
_DynamicDialogState createState() => _DynamicDialogState();
}
class _DynamicDialogState extends State<DynamicDialog> {
@override
Widget build(BuildContext context) {
// You can change the UI as per
// your requirement or choice
return AlertDialog(
title: Text(widget.title),
actions: <Widget>[
OutlinedButton.icon(
label: const Text('Close'),
onPressed: () {
Navigator.pop(context);
},
icon: const Icon(Icons.close))
],
content: Text(widget.body),
);
}
}
Call these functions below the initialization of the local_notification service like this
Dart
@override
void initState() {
super.initState();
LocalNotificationService.initialize();
LocalNotificationService.messageListener(context);
FirebaseMessaging.onBackgroundMessage(backgroundHandler);
FirebaseMessaging.instance.getInitialMessage().then((message) {
print("Initial Message ${message.toString()}");
});
FirebaseMessaging.onMessageOpenedApp.listen((event) {
print("Message on App opened ${event.toString()}");
});
FirebaseMessaging.onMessage.listen((event) {
print("Message when it is termination mode ${event.toString()}");
if (event.notification != null) {
LocalNotificationService.display(event);
}
});
}
Similar Reads
Flutter Tutorial
This Flutter Tutorial is specifically designed for beginners and experienced professionals. It covers both the basics and advanced concepts of the Flutter framework.Flutter is Googleâs mobile SDK that builds native Android and iOS apps from a single codebase. It was developed in December 2017. When
7 min read
Dart Tutorial
Dart is an open-source general-purpose programming language developed by Google. It supports application development on both the client and server side. However, it is widely used for the development of Android apps, iOS apps, IoT(Internet of Things), and web applications using the Flutter Framework
7 min read
Top 50 Flutter Interview Questions and Answers for 2025
Flutter is an open-source, cross-platform application development framework. It was developed by Google in 2017. It is used to build applications for Android, iOS, Linux, Mac, Windows, and the web. Flutter uses the Dart programming language. It provides a simple, powerful, efficient, and easy-to-und
15+ min read
What is Widgets in Flutter?
Flutter is Google's UI toolkit for crafting beautiful, natively compiled iOS and Android apps from a single code base. To build any application we start with widgets - The building block of Flutter applications. Widgets describe what their view should look like given their current configuration and
5 min read
Flutter | An introduction to the open source SDK by Google
Flutter is Googleâs Mobile SDK to build native iOS and Android, Desktop (Windows, Linux, macOS), and Web apps from a single codebase. When building applications with Flutter, everything is Widgets â the blocks with which the flutter apps are built. They are structural elements that ship with a bunch
5 min read
Android Studio Setup for Flutter Development
This article will show how to set up Android Studio to run Flutter Applications. Android Studio is one of the popular IDE( integrated development environment  ) developed by Google itself to create cross-platform Android applications. First, you have to install Android Studio version 3.0 or later, a
3 min read
Dart - Data Types
Like other languages (C, C++, Java), whenever a variable is created, each variable has an associated data type. In Dart language, there are the types of values that can be represented and manipulated in a programming language. In this article, we will learn about Dart Programming Language Data Types
8 min read
Flutter - Architecture Application
Flutter architecture application mainly consists of: WidgetsGesturesConcept of StateLayersWidgetsWidgets are the primary component of any flutter application. It acts as a UI for the user to interact with the application. Any flutter application is itself a widget that is made up of a combination of
3 min read
Dart SDK Installation
To do a lot of interesting programming stuff using the Dart programming language, we have to install the Dart SDK. Dart SDK is a pre-compiled version so we have to download and extract it only. In this article, we will learn how to perform Dart SDK Download.Table of ContentInstall Dart SDK in Window
4 min read
Dart - Standard Input Output
In Dart programming language, you can take standard input from the user through the console via the use of the .readLineSync() function. To take input from the console you need to import a library, named dart:io from the libraries of Dart.Stdin ClassThis class allows the user to read data from stand
3 min read