flutter_local_notifications
repository·master·Indexed 25 days ago
https://github.com/maikub/flutter_local_notificationsA cross-platform Flutter plugin for displaying local notifications on Android, iOS, macOS, Linux, Windows, and Web. It supports basic notifications, scheduling, periodic alerts, and platform-specific features such as Android notification channels, iOS attachments, and Windows toast notifications. Requires Flutter SDK 3.38.1 or newer.
What's inside flutter_local_notifications
- A cross-platform Flutter plugin for displaying local notifications. It supports Android, iOS, macOS, Linux, Windows, and Web. The plugin is designed to be mockable as its API methods are not static, making it suitable for testing.
Use the flutter_local_notifications plugin
masterThe
flutter_local_notificationspackage is the primary cross-platform plugin used to display local notifications within Flutter applications. While the repository contains multiple packages for specific platform implementations (Linux, Windows, Web), most developers should interact directly with the mainflutter_local_notificationspackage.For detailed setup instructions, platform-specific configurations, and feature-specific code samples, refer to the README file located within the
flutter_local_notificationsdirectory or check the provided example app.Schedule a zoned notification
masterTo avoid issues with Daylight Saving Time, use
zonedScheduleinstead of the deprecatedschedulemethod. This requires thetimezonepackage.Steps:
- Add
timezoneas a direct dependency. - Initialize the timezone database with
tz.initializeTimeZones(). - Set the local location using
tz.setLocalLocation(). - Use
tz.TZDateTimeto define the scheduled time.
On Android, use
androidScheduleModeto control precision.AndroidScheduleMode.exactAllowWhileIdleallows exact timing even in low-power modes, but requires the 'exact alarm' permission.import 'package:timezone/data/latest_all.dart' as tz; import 'package:timezone/timezone.dart' as tz; // Initialize tz.initializeTimeZones(); tz.setLocalLocation(tz.getLocation(timeZoneName)); // Schedule await flutterLocalNotificationsPlugin.zonedSchedule( 0, title: 'scheduled title', body: 'scheduled body', scheduledDate: tz.TZDateTime.now(tz.local).add(const Duration(seconds: 5)), const NotificationDetails( android: AndroidNotificationDetails( 'your channel id', 'your channel name', channelDescription: 'your channel description')), androidScheduleMode: AndroidScheduleMode.exactAllowWhileIdle);- Add
Regenerate Dart FFI bindings
masterThe plugin uses
package:ffigento generate Dart bindings from the native C header filesrc/ffi_api.h. If the native API changes, you must regenerate the bindings by running the following command:dart run ffigen --config ffigen.yamlBuild and bundle native Windows code manually
masterThe native C++ code in the
srcdirectory can be built using CMake to generate a DLL. This is primarily useful for local testing outside of the Flutter environment. When developing a standard Flutter app, Flutter handles the building and bundling of these assets automatically.To build manually, use the provided
build.batscript or the following commands:@echo off cd build cmake ../windows cmake --build . cd .. copy build\shared\Debug\flutter_local_notifications_windows.dll .Implement a custom platform for flutter_local_notifications
masterTo create a platform-specific implementation for theflutter_local_notificationsplugin, you must extend theFlutterLocalNotificationsPlatformclass. Once your implementation is ready, you must register it by assigning it to theFlutterLocalNotificationsPlatform.instanceproperty during plugin registration.Configure Notification Icons and Sounds for Android
masterNotification icons should be added as drawable resources. Custom sounds should be added as raw resources.
When using
AndroidNotificationDetails:- Use
DrawableResourceAndroidBitmapto load an icon from a drawable resource. - Use
FilePathAndroidBitmapto load an icon from a file path.
Important: For Android 8.0+, sounds and vibrations are tied to the notification channel. These settings can only be configured when the channel is first created. Subsequent notifications using the same channel ID cannot change these properties.
- Use
Request notification permissions on iOS and macOS
masterTo avoid showing permission prompts immediately upon app launch, initialize the plugin with permission request flags set to
false. You can then callrequestPermissionsat a more appropriate time in your application flow usingresolvePlatformSpecificImplementation.// 1. Initialize with permissions set to false final DarwinInitializationSettings initializationSettingsDarwin = DarwinInitializationSettings( requestSoundPermission: false, requestBadgePermission: false, requestAlertPermission: false, ); // ... initialize plugin ... // 2. Request permissions later final bool result = await flutterLocalNotificationsPlugin .resolvePlatformSpecificImplementation<IOSFlutterLocalNotificationsPlugin>() ?.requestPermissions( alert: true, badge: true, sound: true, );Customize iOS launch screen assets
masterTo customize the iOS launch screen, you can either replace the image files directly within theios/Runner/Assets.xcassets/LaunchImage.imageset/directory or use Xcode to manage the assets. Using Xcode is recommended for easier asset management.Use the Linux implementation of flutter_local_notifications
masterThe Linux implementation is automatically included as a dependency of the mainflutter_local_notificationspackage. You do not need to add it separately; simply useflutter_local_notificationsin your project as usual to enable Linux support.Initialize Flutter Local Notifications
masterTo use the plugin, create an instance of
FlutterLocalNotificationsPluginand initialize it with platform-specific settings. You must provide settings for the platforms you target, or you will encounter a runtimeArgumentError.Key components:
AndroidInitializationSettings: Requires an icon name that must exist as a drawable resource in your Android project.DarwinInitializationSettings: Used for iOS and macOS.LinuxInitializationSettings: Requires adefaultActionName.WindowsInitializationSettings: RequiresappName,appUserModelId, and aguid.onDidReceiveNotificationResponse: An optional callback triggered when a notification is tapped while the app is running. Note that this callback cannot be used to handle cases where the notification launched the app; usegetNotificationAppLaunchDetailsfor that purpose.
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin(); const AndroidInitializationSettings initializationSettingsAndroid = AndroidInitializationSettings('app_icon'); final DarwinInitializationSettings initializationSettingsDarwin = DarwinInitializationSettings(); final LinuxInitializationSettings initializationSettingsLinux = LinuxInitializationSettings( defaultActionName: 'Open notification'); final WindowsInitializationSettings initializationSettingsWindows = WindowsInitializationSettings( appName: 'Flutter Local Notifications Example', appUserModelId: 'Com.Dexterous.FlutterLocalNotificationsExample', guid: 'd49b0314-ee7a-4626-bf79-97cdb8a991bb') final InitializationSettings initializationSettings = InitializationSettings( android: initializationSettingsAndroid, iOS: initializationSettingsDarwin, macOS: initializationSettingsDarwin, linux: initializationSettingsLinux, windows: initializationSettingsWindows); await flutterLocalNotificationsPlugin.initialize( settings: initializationSettings, onDidReceiveNotificationResponse: onDidReceiveNotificationResponse);Configure iOS General Setup
masterTo enable notification handling on iOS, you must set theUNUserNotificationCenterdelegate in yourAppDelegatefile. This allows the plugin to receive notification events.