Flutter System Notifications Examples
Flutter System Notifications - How To Examples
Introduction
Flutter System Notifications is a plugin that allows you to interact with system notifications on both iOS and Android platforms. You can use this plugin to create, update, and delete system notifications, as well as handle user interactions with notifications.
In this tutorial, we will cover various examples that demonstrate how to use the Flutter System Notifications plugin. We will start by installing the plugin and setting up a basic notification, and then move on to more advanced examples.
Installation
To use the Flutter System Notifications plugin, you need to add it as a dependency in your pubspec.yaml
file:
dependencies:
flutter_system_notifications: ^1.0.0
After adding the dependency, run flutter pub get
to download and install the plugin.
Example 1: Creating a Basic Notification
To create a basic notification, you need to import the flutter_system_notifications
package and use the createNotification
method. Here's an example:
import 'package:flutter_system_notifications/flutter_system_notifications.dart';
void createBasicNotification() {
FlutterSystemNotifications.createNotification(
id: 1,
title: 'New Message',
body: 'You have received a new message.',
payload: 'message',
);
}
In this example, we are creating a notification with an ID of 1, a title of "New Message," a body of "You have received a new message," and a payload of "message." The payload can be used to pass additional data when the user interacts with the notification.
Example 2: Creating a Scheduled Notification
You can also schedule a notification to be displayed at a specific time using the createScheduledNotification
method. Here's an example:
import 'package:flutter_system_notifications/flutter_system_notifications.dart';
void createScheduledNotification() {
DateTime scheduledTime = DateTime.now().add(Duration(hours: 1));
FlutterSystemNotifications.createScheduledNotification(
id: 2,
title: 'Scheduled Notification',
body: 'This notification is scheduled to appear in 1 hour.',
payload: 'scheduled',
scheduledTime: scheduledTime,
);
}
In this example, we are scheduling a notification with an ID of 2, a title of "Scheduled Notification," a body of "This notification is scheduled to appear in 1 hour," and a payload of "scheduled." The scheduledTime
parameter is set to the current time plus 1 hour.
Example 3: Updating a Notification
You can update an existing notification using the updateNotification
method. Here's an example:
import 'package:flutter_system_notifications/flutter_system_notifications.dart';
void updateNotification() {
FlutterSystemNotifications.updateNotification(
id: 1,
title: 'Updated Message',
body: 'You have an updated message.',
payload: 'updated',
);
}
In this example, we are updating the notification with an ID of 1 by changing the title to "Updated Message," the body to "You have an updated message," and the payload to "updated."
Example 4: Deleting a Notification
To delete a notification, use the deleteNotification
method. Here's an example:
import 'package:flutter_system_notifications/flutter_system_notifications.dart';
void deleteNotification() {
FlutterSystemNotifications.deleteNotification(1);
}
In this example, we are deleting the notification with an ID of 1.
Example 5: Handling Notification Interactions
You can handle user interactions with notifications by implementing a callback function and registering it using the setNotificationHandler
method. Here's an example:
import 'package:flutter_system_notifications/flutter_system_notifications.dart';
void handleNotification(String payload) {
if (payload == 'message') {
// Handle message notification
} else if (payload == 'scheduled') {
// Handle scheduled notification
} else if (payload == 'updated') {
// Handle updated notification
}
}
void registerNotificationHandler() {
FlutterSystemNotifications.setNotificationHandler(handleNotification);
}
In this example, we have defined a handleNotification
function that takes a payload as a parameter. Depending on the payload value, you can implement different logic to handle different types of notifications. The registerNotificationHandler
function registers the handleNotification
function as the callback for notification interactions.
Example 6: Cancelling a Scheduled Notification
You can cancel a scheduled notification using the cancelScheduledNotification
method. Here's an example:
import 'package:flutter_system_notifications/flutter_system_notifications.dart';
void cancelScheduledNotification() {
FlutterSystemNotifications.cancelScheduledNotification(2);
}
In this example, we are cancelling the scheduled notification with an ID of 2.
Example 7: Getting Pending Notifications
You can retrieve a list of pending notifications using the getPendingNotifications
method. Here's an example:
import 'package:flutter_system_notifications/flutter_system_notifications.dart';
void getPendingNotifications() async {
List<NotificationData> notifications = await FlutterSystemNotifications.getPendingNotifications();
for (NotificationData notification in notifications) {
print('Notification ID: ${notification.id}');
print('Notification Title: ${notification.title}');
print('Notification Body: ${notification.body}');
print('Notification Payload: ${notification.payload}');
}
}
In this example, we are fetching a list of pending notifications and printing their ID, title, body, and payload.
Example 8: Clearing All Notifications
To clear all notifications, use the clearNotifications
method. Here's an example:
import 'package:flutter_system_notifications/flutter_system_notifications.dart';
void clearAllNotifications() {
FlutterSystemNotifications.clearNotifications();
}
In this example, we are clearing all notifications from the system.
Example 9: Handling Notification Clicks
You can handle clicks on notifications when the app is in the foreground by implementing a callback function and registering it using the setNotificationClickHandler
method. Here's an example:
import 'package:flutter_system_notifications/flutter_system_notifications.dart';
void handleNotificationClick(String payload) {
// Handle notification click
}
void registerNotificationClickHandler() {
FlutterSystemNotifications.setNotificationClickHandler(handleNotificationClick);
}
In this example, we have defined a handleNotificationClick
function that takes a payload as a parameter. You can implement logic to handle the notification click based on the payload value. The registerNotificationClickHandler
function registers the handleNotificationClick
function as the callback for notification clicks.
Example 10: Customizing Notification Appearance
You can customize the appearance of notifications by using the NotificationOptions
class and passing it to the createNotification
or createScheduledNotification
methods. Here's an example:
import 'package:flutter_system_notifications/flutter_system_notifications.dart';
void createCustomizedNotification() {
NotificationOptions options = NotificationOptions(
id: 3,
title: 'Custom Notification',
body: 'This is a customized notification.',
payload: 'custom',
color: Colors.blue,
sound: 'notification_sound.mp3',
icon: 'notification_icon',
importance: NotificationImportance.high,
);
FlutterSystemNotifications.createNotificationWithOptions(options);
}
In this example, we are creating a notification with custom options. We have set the notification ID to 3, the title to "Custom Notification," the body to "This is a customized notification," the payload to "custom," the color to blue, the sound to "notification_sound.mp3," the icon to "notification_icon," and the importance to high.
Conclusion
In this tutorial, we have covered various examples that demonstrate how to use the Flutter System Notifications plugin. You have learned how to create basic and scheduled notifications, update and delete notifications, handle notification interactions and clicks, cancel scheduled notifications, retrieve pending notifications, clear all notifications, and customize notification appearance.
You can now leverage the Flutter System Notifications plugin to implement powerful and interactive system notifications in your Flutter applications.