Flutter Local Notifications Manager Examples
Flutter Local Notifications Manager
Introduction
Flutter Local Notifications Manager is a plugin for displaying local notifications in Flutter applications. It provides an easy way to schedule, display, and handle user interactions with notifications.
In this tutorial, we will cover the installation process, basic usage, and several examples to demonstrate the different features of the plugin.
Installation
To use the Flutter Local Notifications Manager plugin, follow these steps:
- Open your Flutter project in your preferred IDE.
- Open the
pubspec.yaml
file. - Add the following line to the dependencies section:
flutter_local_notifications_manager: ^1.0.0
- Save the file and run
flutter pub get
in the terminal to fetch the package. - Import the package in your Dart file:
import 'package:flutter_local_notifications_manager/flutter_local_notifications_manager.dart';
Basic Usage
Before we dive into the examples, let's cover the basic usage of the plugin. The Flutter Local Notifications Manager provides a NotificationManager
class that allows you to schedule and display notifications.
To schedule a notification, use the scheduleNotification
method:
NotificationManager.scheduleNotification(
id: 1,
title: 'Notification Title',
body: 'Notification Body',
scheduledDate: DateTime.now().add(Duration(seconds: 5)),
);
To display an immediate notification, use the displayNotification
method:
NotificationManager.displayNotification(
id: 2,
title: 'Immediate Notification',
body: 'This notification will be displayed immediately.',
);
To handle user interactions with notifications, you can specify a callback function using the onNotificationClick
parameter:
NotificationManager.scheduleNotification(
id: 3,
title: 'Clickable Notification',
body: 'Click me!',
scheduledDate: DateTime.now().add(Duration(seconds: 10)),
onNotificationClick: (int id) {
print('Notification $id clicked!');
},
);
Now that we understand the basics, let's move on to some examples.
Example 1: Displaying a Simple Notification
In this example, we will display a simple notification with a title and body.
NotificationManager.displayNotification(
id: 1,
title: 'Simple Notification',
body: 'This is a simple notification.',
);
A notification will be displayed on the user's device with the specified title and body.
Example 2: Scheduling a Notification with a Specific Date and Time
In this example, we will schedule a notification to be displayed at a specific date and time.
NotificationManager.scheduleNotification(
id: 2,
title: 'Scheduled Notification',
body: 'This notification is scheduled for a specific date and time.',
scheduledDate: DateTime(2022, 12, 31, 23, 59, 59),
);
The notification will be displayed on the user's device on December 31, 2022, at 11:59:59 PM.
Example 3: Repeating a Notification
In this example, we will schedule a notification to be displayed at regular intervals.
NotificationManager.scheduleNotification(
id: 3,
title: 'Repeating Notification',
body: 'This notification will repeat every hour.',
scheduledDate: DateTime.now(),
repeatInterval: RepeatInterval.Hourly,
);
The notification will be displayed on the user's device every hour.
Example 4: Customizing Notification Appearance
In this example, we will customize the appearance of a notification by specifying an icon, color, and sound.
NotificationManager.displayNotification(
id: 4,
title: 'Custom Notification',
body: 'This notification has a custom icon, color, and sound.',
icon: 'app_icon',
color: Colors.blue,
sound: 'custom_sound.mp3',
);
A notification with a custom icon, blue color, and a custom sound will be displayed on the user's device.
Example 5: Handling Notification Clicks
In this example, we will handle user interactions with a notification by specifying a callback function.
NotificationManager.scheduleNotification(
id: 5,
title: 'Clickable Notification',
body: 'Click me!',
scheduledDate: DateTime.now(),
onNotificationClick: (int id) {
print('Notification $id clicked!');
},
);
When the user clicks on the notification, the specified callback function will be called, and the message "Notification 5 clicked!" will be printed.
Example 6: Canceling a Scheduled Notification
In this example, we will cancel a scheduled notification.
NotificationManager.cancelNotification(2);
The scheduled notification with ID 2 will be canceled and will not be displayed on the user's device.
Example 7: Canceling All Scheduled Notifications
In this example, we will cancel all scheduled notifications.
NotificationManager.cancelAllNotifications();
All scheduled notifications will be canceled and will not be displayed on the user's device.
Example 8: Checking If a Notification Is Scheduled
In this example, we will check if a notification with a specific ID is scheduled.
bool isScheduled = await NotificationManager.isNotificationScheduled(3);
print('Notification with ID 3 is scheduled: $isScheduled');
The value of isScheduled
will be true
if a notification with ID 3 is scheduled, and false
otherwise.
Example 9: Checking If a Notification Is Displayed
In this example, we will check if a notification with a specific ID is currently being displayed.
bool isDisplayed = await NotificationManager.isNotificationDisplayed(4);
print('Notification with ID 4 is displayed: $isDisplayed');
The value of isDisplayed
will be true
if a notification with ID 4 is currently being displayed, and false
otherwise.
Example 10: Retrieving a List of Scheduled Notifications
In this example, we will retrieve a list of all scheduled notifications.
List<Notification> scheduledNotifications = await NotificationManager.getScheduledNotifications();
print('Scheduled notifications: $scheduledNotifications');
The list scheduledNotifications
will contain all the scheduled notifications, including their IDs, titles, bodies, and scheduled dates.
These examples cover some of the main features of the Flutter Local Notifications Manager plugin. You can explore the documentation for more advanced usage and customization options.