Background local notifications in Flutter with Example

Show background local notifications in Flutter using the flutter_local_notifications package:

Getting Started

Create a new Flutter project in your preferred IDE.

Add the flutter_local_notifications package to your pubspec.yaml file:

dependencies:
  flutter_local_notifications: ^8.2.0

Import the package in your Dart code:

import 'package:flutter_local_notifications/flutter_local_notifications.dart';

Showing Background Local Notifications

To show background local notifications in Flutter, follow these steps:

Define a FlutterLocalNotificationsPlugin variable in your stateful widget:

FlutterLocalNotificationsPlugin _notificationsPlugin;

Override the initState method to initialize the notifications plugin:

@override
void initState() {
  super.initState();
  _initializeNotifications();
}

Define a private _initializeNotifications method to set up the notifications plugin:

Future<void> _initializeNotifications() async {
  final initializationSettings = InitializationSettings(
    android: AndroidInitializationSettings('@mipmap/ic_launcher'),
  );
  _notificationsPlugin = FlutterLocalNotificationsPlugin();
  await _notificationsPlugin.initialize(initializationSettings);
}

Define a private _showNotification method to show a notification:

Future<void> _showNotification() async {
  const androidPlatformChannelSpecifics = AndroidNotificationDetails(
    'channel_id',
    'channel_name',
    'channel_description',
    importance: Importance.max,
    priority: Priority.high,
    showWhen: false,
  );
  const platformChannelSpecifics =
      NotificationDetails(android: androidPlatformChannelSpecifics);
  await _notificationsPlugin.show(
    0,
    'Title',
    'Body',
    platformChannelSpecifics,
    payload: 'Notification Payload',
  );
}

Call the _showNotification method whenever you want to show a notification:

FloatingActionButton(
  onPressed: _showNotification,
  child: Icon(Icons.notifications),
)

Set up a background task to show notifications while your app is in the background. Here's an example of how to do this with the flutter_background and flutter_workmanager packages:

dependencies:
  flutter_background: ^1.0.7
  flutter_workmanager: ^0.3.1
import 'package:flutter_background/flutter_background.dart';
import 'package:flutter_workmanager/flutter_workmanager.dart';
void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await FlutterBackground.initialize();
  await Workmanager.initialize(
    callbackDispatcher,
    isInDebugMode: false,
  );
  await Workmanager.registerOneOffTask(
    'show_notification',
    'show_notification_task',
    inputData: <String, dynamic>{},
    initialDelay: Duration(seconds: 10),
  );
  runApp(MyApp());
}
void callbackDispatcher() {
  Workmanager.executeTask((taskName, inputData) async {
    final notificationPlugin = FlutterLocalNotificationsPlugin();
    const androidSettings = AndroidInitializationSettings('@mipmap/ic_launcher');
    await notificationPlugin.initialize(InitializationSettings(
      android: androidSettings,
    ));
    const androidDetails = AndroidNotificationDetails(
      'channel_id',
      'channel_name',
      'channel_description',
      importance: Importance.max,
      priority: Priority.high,
      showWhen: false,
    );
    const notificationDetails = NotificationDetails(
      android: androidDetails,
    );
    await notificationPlugin.show(
      0,
      'Title',
      'Body',
      notificationDetails,
      payload: 'Notification Payload',
    );
    return Future.value(true);
  });
}

That's it! You can now show