Search code examples
androidiosflutterflutter-local-notification

How to schedule multiple time specific local notifications in flutter


I am developing water drinking reminder app with Flutter. I want to schedule a list of time specified local notifications that user can add to this list and delete from this list.

enter image description here


Solution

  • I have solved this problem. The full code is below.

    import 'package:flutter_local_notifications/flutter_local_notifications.dart';
    import 'package:flutter_native_timezone/flutter_native_timezone.dart';
    import 'package:timezone/data/latest.dart' as tz;
    import 'package:timezone/timezone.dart' as tz;
    
    class NotificationHelper {
      FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
          FlutterLocalNotificationsPlugin();
    
      /// Initialize notification
      initializeNotification() async {
        _configureLocalTimeZone();
        const IOSInitializationSettings initializationSettingsIOS = IOSInitializationSettings();
    
        const AndroidInitializationSettings initializationSettingsAndroid =
            AndroidInitializationSettings("ic_launcher");
    
        const InitializationSettings initializationSettings = InitializationSettings(
          iOS: initializationSettingsIOS,
          android: initializationSettingsAndroid,
        );
        await flutterLocalNotificationsPlugin.initialize(initializationSettings);
      }
    
      /// Set right date and time for notifications
      tz.TZDateTime _convertTime(int hour, int minutes) {
        final tz.TZDateTime now = tz.TZDateTime.now(tz.local);
        tz.TZDateTime scheduleDate = tz.TZDateTime(
          tz.local,
          now.year,
          now.month,
          now.day,
          hour,
          minutes,
        );
        if (scheduleDate.isBefore(now)) {
          scheduleDate = scheduleDate.add(const Duration(days: 1));
        }
        return scheduleDate;
      }
    
      Future<void> _configureLocalTimeZone() async {
        tz.initializeTimeZones();
        final String timeZone = await FlutterNativeTimezone.getLocalTimezone();
        tz.setLocalLocation(tz.getLocation(timeZone));
      }
    
      /// Scheduled Notification
      scheduledNotification({
        required int hour,
        required int minutes,
        required int id,
        required String sound,
      }) async {
        await flutterLocalNotificationsPlugin.zonedSchedule(
          id,
          'It\'s time to drink water!',
          'After drinking, touch the cup to confirm',
          _convertTime(hour, minutes),
          NotificationDetails(
            android: AndroidNotificationDetails(
              'your channel id $sound',
              'your channel name',
              channelDescription: 'your channel description',
              importance: Importance.max,
              priority: Priority.high,
              sound: RawResourceAndroidNotificationSound(sound),
            ),
            iOS: IOSNotificationDetails(sound: '$sound.mp3'),
          ),
          androidAllowWhileIdle: true,
          uiLocalNotificationDateInterpretation: UILocalNotificationDateInterpretation.absoluteTime,
          matchDateTimeComponents: DateTimeComponents.time,
          payload: 'It could be anything you pass',
        );
      }
    
      /// Request IOS permissions
      void requestIOSPermissions() {
        flutterLocalNotificationsPlugin
            .resolvePlatformSpecificImplementation<IOSFlutterLocalNotificationsPlugin>()
            ?.requestPermissions(
              alert: true,
              badge: true,
              sound: true,
            );
      }
    
      cancelAll() async => await flutterLocalNotificationsPlugin.cancelAll();
      cancel(id) async => await flutterLocalNotificationsPlugin.cancel(id);
    }
    

    add your custom times like this

    for (int i = 0; i < _provider.getScheduleRecords.length; i++) {
      _notificationHelper.scheduledNotification(
        hour: int.parse(_provider.getScheduleRecords[i].time.split(":")[0]),
        minutes: int.parse(_provider.getScheduleRecords[i].time.split(":")[1]),
        id: _provider.getScheduleRecords[i].id,
        sound: 'sound0',
      );
    }