Local notifications help your app tell users important things even when they are not using the app. They grab attention and remind users about events or updates.
Notifications (local) in Flutter
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin(); // Initialize in main or initState var initializationSettingsAndroid = AndroidInitializationSettings('app_icon'); var initializationSettings = InitializationSettings(android: initializationSettingsAndroid); await flutterLocalNotificationsPlugin.initialize(initializationSettings); // Show notification await flutterLocalNotificationsPlugin.show( 0, // notification id 'Title', 'Body text', NotificationDetails(android: AndroidNotificationDetails('channelId', 'channelName', importance: Importance.max, priority: Priority.high)) );
You must initialize the notification plugin before showing notifications.
Android requires a notification channel with an ID and name.
await flutterLocalNotificationsPlugin.show( 1, 'Hello', 'This is a simple notification', NotificationDetails(android: AndroidNotificationDetails('channel1', 'General', importance: Importance.defaultImportance, priority: Priority.defaultPriority)) );
var androidDetails = AndroidNotificationDetails( 'reminder_channel', 'Reminders', importance: Importance.high, priority: Priority.high ); await flutterLocalNotificationsPlugin.show( 2, 'Reminder', 'Don\'t forget your meeting at 3 PM', NotificationDetails(android: androidDetails) );
This app shows a button. When pressed, it sends a local notification with a title and message.
import 'package:flutter/material.dart'; import 'package:flutter_local_notifications/flutter_local_notifications.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatefulWidget { const MyApp({super.key}); @override State<MyApp> createState() => _MyAppState(); } class _MyAppState extends State<MyApp> { final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin(); @override void initState() { super.initState(); var initializationSettingsAndroid = const AndroidInitializationSettings('app_icon'); var initializationSettings = InitializationSettings(android: initializationSettingsAndroid); flutterLocalNotificationsPlugin.initialize(initializationSettings); } Future<void> _showNotification() async { var androidDetails = const AndroidNotificationDetails( 'channelId', 'channelName', importance: Importance.max, priority: Priority.high, ); var generalNotificationDetails = NotificationDetails(android: androidDetails); await flutterLocalNotificationsPlugin.show( 0, 'Hello!', 'This is a local notification.', generalNotificationDetails, ); } @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: const Text('Local Notifications Example')), body: Center( child: ElevatedButton( onPressed: _showNotification, child: const Text('Show Notification'), ), ), ), ); } }
On Android, you need to add an app icon named 'app_icon' in your drawable folder for notifications.
Local notifications do not require internet access.
Test notifications on a real device for best results, as emulators may not show them properly.
Local notifications alert users even when the app is not open.
Initialize the notification plugin before sending notifications.
Customize notifications with channels, importance, and priority for better user experience.
Practice
Solution
Step 1: Understand local notifications
Local notifications are alerts shown to users even if the app is not open or running in the background.Step 2: Compare options
Options B, C, and D describe other app features unrelated to local notifications.Final Answer:
To alert users even when the app is closed or in the background -> Option AQuick Check:
Local notifications alert users outside app use = A [OK]
- Confusing local notifications with server messages
- Thinking notifications only work when app is open
- Mixing UI updates with notification purpose
Solution
Step 1: Identify notification plugins
The plugin flutter_local_notifications is designed for local notifications in Flutter apps.Step 2: Eliminate unrelated plugins
http is for network calls, provider for state management, firebase_messaging for push notifications, not local.Final Answer:
flutter_local_notifications -> Option BQuick Check:
Local notifications plugin = flutter_local_notifications [OK]
- Choosing firebase_messaging which is for push, not local
- Confusing state management plugins with notification plugins
- Selecting network plugins by mistake
show() on a local notification without initializing the plugin first?Solution
Step 1: Understand plugin initialization
Local notification plugins require initialization before use to set up channels and permissions.Step 2: Consequence of missing initialization
Calling show() without initialization causes runtime errors or crashes because the plugin is not ready.Final Answer:
The app will crash or throw an error -> Option DQuick Check:
Uninitialized plugin causes error/crash = B [OK]
- Assuming notifications show without initialization
- Thinking notifications delay automatically
- Believing platform-specific behavior without init
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// Missing initialization here
await flutterLocalNotificationsPlugin.show(
0,
'Hello',
'Welcome to Flutter',
NotificationDetails(
android: AndroidNotificationDetails('channelId', 'channelName')
)
);
}Solution
Step 1: Check plugin initialization
The code calls show() without calling initialize() on flutterLocalNotificationsPlugin first.Step 2: Understand consequences
Without initialize(), the plugin is not ready, causing errors when showing notifications.Final Answer:
Missing call to initialize() before show() -> Option CQuick Check:
Initialize plugin before show() = C [OK]
- Ignoring the need to initialize plugin
- Thinking zero ID is invalid
- Assuming ensureInitialized() replaces plugin init
Solution
Step 1: Understand importance and priority
High importance and priority make notifications more visible and urgent on Android.Step 2: Check code options
AndroidNotificationDetails('channelId', 'channelName', importance: Importance.high, priority: Priority.high) sets importance and priority to high, matching the requirement. Others set low, default, or omit these settings.Final Answer:
AndroidNotificationDetails('channelId', 'channelName', importance: Importance.high, priority: Priority.high) -> Option AQuick Check:
High importance and priority = AndroidNotificationDetails('channelId', 'channelName', importance: Importance.high, priority: Priority.high) [OK]
- Leaving importance and priority at default
- Using low importance for urgent alerts
- Omitting priority setting
Ready to prove it?
Pass the test and this pattern moves unknown
