Local Notifications - schedule

  1. Initialize the Plugin
    The initializeTimeZone must be initialized before use. Add this code somewhere in your app (e.g., in main.dart or a separate setup function):

    add custom action initializeTimeZoneto Initial Actions at main.dart
    timezone: ^0.10.0

    // Required imports for timezone package import 'package:timezone/timezone.dart' as tz; import 'package:timezone/data/latest_all.dart' as tz; // Action to initialize time zones Future initializeTimeZone() async { // Load the timezone database tz.initializeTimeZones(); }

  2. Add custom actions initializeNotifications at home page loading

    flutter_local_notifications: ^18.0.1

    
    import 'package:flutter_local_notifications/flutter_local_notifications.dart';
    import 'package:timezone/timezone.dart' as tz;
    
    final flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
    
    Future<void> initializeNotifications() async {
      final androidSettings = AndroidInitializationSettings('@mipmap/ic_launcher');
      final iosSettings = DarwinInitializationSettings();
      await flutterLocalNotificationsPlugin.initialize(
          InitializationSettings(
            android: androidSettings,
            iOS: iosSettings,
          ), onDidReceiveNotificationResponse: (payload) {
        if (payload != null) {
          print('Notification Response: $payload');
        }
      });
    
      // Create the notification channel for Android
      const AndroidNotificationChannel channel = AndroidNotificationChannel(
        'channel_id',
        'channel_name',
        description: 'channel_description',
        importance: Importance.max,
        playSound: true,
      );
      await flutterLocalNotificationsPlugin
          .resolvePlatformSpecificImplementation<
              AndroidFlutterLocalNotificationsPlugin>()
          ?.createNotificationChannel(channel);
    
      // Request permissions for iOS
      await flutterLocalNotificationsPlugin
          .resolvePlatformSpecificImplementation<
              IOSFlutterLocalNotificationsPlugin>()
          ?.requestPermissions(
            alert: true,
            badge: true,
            sound: true,
          );
    }
  3. Use this custom actions scheduleNotification to set notification

    
    import 'package:flutter_local_notifications/flutter_local_notifications.dart';
    import 'package:timezone/timezone.dart' as tz;
    
    // Define this globally (outside the function) and initialize it elsewhere
    final flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
    
    Future scheduleNotification(
      String title,
      String body,
      DateTime scheduleTime, // Already a DateTime, no parsing needed
    ) async {
      // Get the local time zone
      final localTz = tz.local;
    
      // Convert scheduleTime to TZDateTime for timezone-aware scheduling
      final tzScheduleTime = tz.TZDateTime.from(scheduleTime, localTz);
    
      // Schedule the notification
      await flutterLocalNotificationsPlugin.zonedSchedule(
        0, // notificationId
        title,
        body,
        tzScheduleTime,
        const NotificationDetails(
          android: AndroidNotificationDetails(
            'channel_id',
            'channel_name',
            importance: Importance.max,
            priority: Priority.high,
          ),
          iOS: DarwinNotificationDetails(),
        ),
        androidScheduleMode: AndroidScheduleMode.exact, // Added required parameter
        uiLocalNotificationDateInterpretation:
            UILocalNotificationDateInterpretation.absoluteTime,
      );
    }


    I tested it's work on iOS - real device.

    You can check this app has local notification here: https://apps.apple.com/us/app/speak-english-with-verbally-ai/id6740076311




8
1 reply