Flutter Notifications Examples
Flutter Notifications - How To Examples
Introduction
In Flutter, notifications play an important role in keeping users informed about important events or updates within an app. The flutter_notifications
package provides a convenient way to send and receive notifications in your Flutter applications. This tutorial will guide you through the installation process and provide step-by-step examples for working with notifications in Flutter.
Installation
To use the flutter_notifications
package, you need to add it as a dependency in your Flutter project. Follow these steps to install it:
- Open your project's
pubspec.yaml
file. - Add
flutter_notifications: ^1.0.0
under thedependencies
section. - Save the file and run
flutter pub get
in the terminal to fetch the package.
Example 1: Displaying a Local Notification
In this example, we will demonstrate how to display a local notification in Flutter.
import 'package:flutter/material.dart';
import 'package:flutter_notifications/flutter_notifications.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
final FlutterNotifications flutterNotifications = FlutterNotifications();
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Flutter Notifications'),
),
body: Center(
child: RaisedButton(
child: Text('Show Notification'),
onPressed: () {
_showNotification();
},
),
),
),
);
}
void _showNotification() async {
await flutterNotifications.show(
title: 'New Notification',
content: 'You have a new message.',
id: 1,
);
}
}
When you run the app and tap the "Show Notification" button, a local notification with the title "New Notification" and content "You have a new message." will be displayed on the device.
Example 2: Handling Tapped Notifications
In this example, we will handle the tap action on a notification and navigate to a specific screen in the app.
import 'package:flutter/material.dart';
import 'package:flutter_notifications/flutter_notifications.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
final FlutterNotifications flutterNotifications = FlutterNotifications();
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Flutter Notifications'),
),
body: Center(
child: RaisedButton(
child: Text('Show Notification'),
onPressed: () {
_showNotification();
},
),
),
),
onGenerateRoute: (settings) {
if (settings.name == '/details') {
return MaterialPageRoute(
builder: (context) => DetailsScreen(),
);
}
return null;
},
);
}
void _showNotification() async {
await flutterNotifications.show(
title: 'New Notification',
content: 'You have a new message.',
id: 1,
payload: '/details',
);
}
}
class DetailsScreen extends StatelessWidget {
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Details Screen'),
),
body: Center(
child: Text('Welcome to the Details Screen!'),
),
);
}
}
When you tap on the notification, the app will navigate to the DetailsScreen
and display the message "Welcome to the Details Screen!".
Example 3: Handling Notification Actions
In this example, we will handle different actions associated with a notification, such as opening a specific screen or performing some action.
import 'package:flutter/material.dart';
import 'package:flutter_notifications/flutter_notifications.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
final FlutterNotifications flutterNotifications = FlutterNotifications();
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Flutter Notifications'),
),
body: Center(
child: RaisedButton(
child: Text('Show Notification'),
onPressed: () {
_showNotification();
},
),
),
),
onGenerateRoute: (settings) {
if (settings.name == '/details') {
return MaterialPageRoute(
builder: (context) => DetailsScreen(),
);
}
return null;
},
);
}
void _showNotification() async {
await flutterNotifications.show(
title: 'New Notification',
content: 'You have a new message.',
id: 1,
payload: '/details',
actions: [
NotificationAction(
title: 'Reply',
icon: 'reply_icon',
action: '/reply',
),
NotificationAction(
title: 'Dismiss',
icon: 'dismiss_icon',
action: '/dismiss',
),
],
);
}
}
class DetailsScreen extends StatelessWidget {
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Details Screen'),
),
body: Center(
child: Text('Welcome to the Details Screen!'),
),
);
}
}
When you tap on the notification, the app will navigate to the DetailsScreen
and display the message "Welcome to the Details Screen!". The notification will also contain two action buttons: "Reply" and "Dismiss". Tapping on these buttons will trigger the respective actions defined in the NotificationAction
objects.
Example 4: Scheduling Local Notifications
In this example, we will schedule a local notification to be displayed at a specific date and time.
import 'package:flutter/material.dart';
import 'package:flutter_notifications/flutter_notifications.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
final FlutterNotifications flutterNotifications = FlutterNotifications();
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Flutter Notifications'),
),
body: Center(
child: RaisedButton(
child: Text('Schedule Notification'),
onPressed: () {
_scheduleNotification();
},
),
),
),
);
}
void _scheduleNotification() async {
final scheduledDate = DateTime.now().add(Duration(minutes: 5));
await flutterNotifications.schedule(
title: 'Scheduled Notification',
content: 'This notification was scheduled.',
id: 1,
scheduledDate: scheduledDate,
);
}
}
When you tap the "Schedule Notification" button, a local notification with the title "Scheduled Notification" and content "This notification was scheduled." will be displayed on the device after 5 minutes.
Example 5: Canceling Local Notifications
In this example, we will cancel a scheduled local notification.
import 'package:flutter/material.dart';
import 'package:flutter_notifications/flutter_notifications.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
final FlutterNotifications flutterNotifications = FlutterNotifications();
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Flutter Notifications'),
),
body: Center(
child: RaisedButton(
child: Text('Cancel Notification'),
onPressed: () {
_cancelNotification();
},
),
),
),
);
}
void _cancelNotification() async {
await flutterNotifications.cancel(1);
}
}
When you tap the "Cancel Notification" button, any scheduled or displayed notification with the ID 1
will be canceled and removed from the device's notification center.
Example 6: Displaying a Big Picture Style Notification
In this example, we will display a notification with a big picture style, suitable for showing images or thumbnails.
import 'package:flutter/material.dart';
import 'package:flutter_notifications/flutter_notifications.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
final FlutterNotifications flutterNotifications = FlutterNotifications();
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Flutter Notifications'),
),
body: Center(
child: RaisedButton(
child: Text('Show Notification'),
onPressed: () {
_showNotification();
},
),
),
),
);
}
void _showNotification() async {
await flutterNotifications.show(
title: 'New Notification',
content: 'You have a new message.',
id: 1,
style: NotificationStyle.bigPicture(
bigPicture: 'https://example.com/image.jpg',
),
);
}
}
When you tap the "Show Notification" button, a notification with a big picture style will be displayed. The big picture will be fetched from the provided URL (https://example.com/image.jpg
) and shown alongside the notification content.
Example 7: Displaying a Progress Indicator in a Notification
In this example, we will display a notification with a progress indicator, useful for showing the progress of a task or download.
import 'package:flutter/material.dart';
import 'package:flutter_notifications/flutter_notifications.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
final FlutterNotifications flutterNotifications = FlutterNotifications();
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Flutter Notifications'),
),
body: Center(
child: RaisedButton(
child: Text('Show Notification'),
onPressed: () {
_showNotification();
},
),
),
),
);
}
void _showNotification() async {
await flutterNotifications.show(
title: 'Download Progress',
content: 'Downloading file...',
id: 1,
style: NotificationStyle.progress(
progress: 0.5,
),
);
}
}
When you tap the "Show Notification" button, a notification with a progress indicator will be displayed. The progress will be set to 0.5
, indicating a halfway completed task or download.
Example 8: Displaying a Messaging Style Notification
In this example, we will display a notification with a messaging style, suitable for chat or messaging apps.
import 'package:flutter/material.dart';
import 'package:flutter_notifications/flutter_notifications.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
final FlutterNotifications flutterNotifications = FlutterNotifications();
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Flutter Notifications'),
),
body: Center(
child: RaisedButton(
child: Text('Show Notification'),
onPressed: () {
_showNotification();
},
),
),
),
);
}
void _showNotification() async {
await flutterNotifications.show(
title: 'New Message',
content: 'You have a new message.',
id: 1,
style: NotificationStyle.messaging(
messages: [
NotificationMessage(
sender: 'John Doe',
content: 'Hey, how are you?',
),
NotificationMessage(
sender: 'Jane Smith',
content: 'I am doing great!',
),
],
),
);
}
}
When you tap the "Show Notification" button, a notification with a messaging style will be displayed. The notification will show the sender name and message content for each message provided in the NotificationMessage
objects.
Example 9: Customizing Notification Appearance
In this example, we will customize the appearance of a notification using custom icons, colors, and sounds.
import 'package:flutter/material.dart';
import 'package:flutter_notifications/flutter_notifications.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
final FlutterNotifications flutterNotifications = FlutterNotifications();
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Flutter Notifications'),
),
body: Center(
child: RaisedButton(
child: Text('Show Notification'),
onPressed: () {
_showNotification();
},
),
),
),
);
}
void _showNotification() async {
await flutterNotifications.show(
title: 'Custom Notification',
content: 'This is a custom notification.',
id: 1,
icon: 'custom_icon',
color: Colors.red,
sound: 'custom_sound',
);
}
}
When you tap the "Show Notification" button, a custom notification will be displayed with a custom icon (assuming an icon with the name custom_icon
is available), a red color, and a custom sound (assuming a sound asset with the name custom_sound
is available).
Example 10: Handling Notification Lifecycles
In this example, we will handle the lifecycle events of a notification, such as when it is displayed, updated, or dismissed.
import 'package:flutter/material.dart';
import 'package:flutter_notifications/flutter_notifications.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
final FlutterNotifications flutterNotifications = FlutterNotifications();
Widget build(BuildContext context) {
flutterNotifications.onNotificationDisplayed.listen((notification) {
print('Notification displayed: ${notification.title}');
});
flutterNotifications.onNotificationUpdated.listen((notification) {
print('Notification updated: ${notification.title}');
});
flutterNotifications.onNotificationDismissed.listen((notification) {
print('Notification dismissed: ${notification.title}');
});
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Flutter Notifications'),
),
body: Center(
child: RaisedButton(
child: Text('Show Notification'),
onPressed: () {
_showNotification();
},
),
),
),
);
}
void _showNotification() async {
await flutterNotifications.show(
title: 'New Notification',
content: 'You have a new message.',
id: 1,
);
}
}
When you tap the "Show Notification" button, a notification will be displayed and the corresponding lifecycle events will be logged in the console.
Conclusion
In this tutorial, you learned how to integrate the flutter_notifications
package into your Flutter applications. You explored various examples to display local notifications, handle tapped notifications, schedule notifications, cancel notifications, customize notification appearance, and handle notification lifecycles. With these examples, you can now implement notifications in your Flutter apps and enhance the user experience.