Skip to main content

Flutter Background Notifications Examples

Flutter Background Notifications - How To Examples

Introduction

Flutter Background Notifications is a Flutter plugin that allows you to receive and handle background push notifications on both Android and iOS platforms. This plugin simplifies the process of setting up and handling push notifications in your Flutter application, making it easier to keep your users engaged and informed.

In this tutorial, we will cover the installation process and provide you with 10 examples of how to use the Flutter Background Notifications plugin in different scenarios. Each example will be accompanied by the expected output to help you understand the functionality better.

Installation

To get started with Flutter Background Notifications, follow these steps:

  1. Open your Flutter project in your preferred code editor.

  2. Open the pubspec.yaml file.

  3. Add the following line to the dependencies section of the file:

    flutter_background_notifications: ^1.0.0
  4. Save the file and run the following command in your terminal to fetch the plugin:

    flutter pub get
  5. Import the plugin in your Dart code:

    import 'package:flutter_background_notifications/flutter_background_notifications.dart';
  6. You are now ready to use the Flutter Background Notifications plugin in your Flutter application.

Example 1: Displaying a Local Notification

In this example, we will show you how to display a local notification using the Flutter Background Notifications plugin.

Future<void> displayLocalNotification() async {
await FlutterBackgroundNotifications.showNotification(
title: 'New Message',
body: 'You have a new message',
androidTitle: 'New Message',
androidDescription: 'You have a new message',
iOSBadge: '1',
);
}
  • On Android, a notification will be displayed in the notification tray with the title 'New Message' and the body 'You have a new message'.
  • On iOS, a notification badge with the number '1' will be displayed on the app icon.

Example 2: Handling a Background Notification Click

In this example, we will show you how to handle a click on a background notification using the Flutter Background Notifications plugin.

void handleNotificationClick() {
FlutterBackgroundNotifications.onNotificationClick.listen((String payload) {
// Handle the notification click event
print('Clicked on notification with payload: $payload');
});
}
  • The payload of the clicked notification will be printed to the console.

Example 3: Scheduling a Local Notification

In this example, we will show you how to schedule a local notification using the Flutter Background Notifications plugin.

Future<void> scheduleLocalNotification() async {
await FlutterBackgroundNotifications.scheduleNotification(
title: 'Reminder',
body: 'Don't forget to take your medication',
androidTitle: 'Reminder',
androidDescription: 'Don't forget to take your medication',
iOSBadge: '1',
scheduledDateTime: DateTime.now().add(Duration(hours: 1)),
);
}
  • A notification will be scheduled to trigger 1 hour from the current time with the title 'Reminder' and the body 'Don't forget to take your medication'.

Example 4: Clearing a Local Notification

In this example, we will show you how to clear a local notification using the Flutter Background Notifications plugin.

Future<void> clearLocalNotification() async {
await FlutterBackgroundNotifications.clearNotification();
}
  • The currently displayed notification will be cleared from the notification tray.

Example 5: Checking Notification Permission Status

In this example, we will show you how to check the notification permission status using the Flutter Background Notifications plugin.

Future<bool> checkNotificationPermissionStatus() async {
final bool isEnabled = await FlutterBackgroundNotifications.isNotificationEnabled();
return isEnabled;
}
  • The function will return a boolean value indicating whether the notification permission is enabled or disabled.

Example 6: Enabling Background Notifications

In this example, we will show you how to enable background notifications using the Flutter Background Notifications plugin.

Future<void> enableBackgroundNotifications() async {
await FlutterBackgroundNotifications.enableBackgroundNotifications();
}
  • Background notifications will be enabled for your Flutter application.

Example 7: Disabling Background Notifications

In this example, we will show you how to disable background notifications using the Flutter Background Notifications plugin.

Future<void> disableBackgroundNotifications() async {
await FlutterBackgroundNotifications.disableBackgroundNotifications();
}
  • Background notifications will be disabled for your Flutter application.

Example 8: Handling a Scheduled Notification Trigger

In this example, we will show you how to handle a scheduled notification trigger using the Flutter Background Notifications plugin.

void handleScheduledNotificationTrigger() {
FlutterBackgroundNotifications.onScheduledNotificationTrigger
.listen((String payload) {
// Handle the scheduled notification trigger event
print('Scheduled notification triggered with payload: $payload');
});
}
  • The payload of the triggered scheduled notification will be printed to the console.

Example 9: Canceling a Scheduled Notification

In this example, we will show you how to cancel a scheduled notification using the Flutter Background Notifications plugin.

Future<void> cancelScheduledNotification() async {
await FlutterBackgroundNotifications.cancelScheduledNotification();
}
  • The currently scheduled notification will be canceled.

Example 10: Retrieving the Initial Notification

In this example, we will show you how to retrieve the initial notification that launched the Flutter application using the Flutter Background Notifications plugin.

Future<String> getInitialNotification() async {
final String payload =
await FlutterBackgroundNotifications.getInitialNotification();
return payload;
}
  • The function will return the payload of the initial notification that launched the Flutter application.

Conclusion

In this tutorial, we have covered the installation process of the Flutter Background Notifications plugin and provided you with 10 examples of how to use the plugin in various scenarios. By following these examples, you can effectively handle background push notifications in your Flutter application and provide a seamless user experience.

Remember to refer to the plugin's documentation for more detailed information and additional features that may not be covered in this tutorial.