【问题标题】:How to schedule multiple time specific local notifications in flutter如何在颤动中安排多个时间特定的本地通知
【发布时间】:2022-07-08 04:43:54
【问题描述】:

我正在开发带有颤振的饮水提醒应用程序。 我想安排一个时间指定的本地通知列表,用户可以将其添加到此列表并从此列表中删除。 like this

任何帮助将不胜感激,谢谢。

【问题讨论】:

    标签: android ios flutter flutter-local-notification


    【解决方案1】:

    你可以使用flutter_local_notifications插件,它可以发送预定的、即时的和重复的通知

    await flutterLocalNotificationsPlugin.zonedSchedule(
    0,
    'scheduled title',
    'scheduled body',
    tz.TZDateTime.now(tz.local).add(const Duration(seconds: 5)),
    const NotificationDetails(
        android: AndroidNotificationDetails(
            'your channel id', 'your channel name',
            channelDescription: 'your channel description')),
    androidAllowWhileIdle: true,
    uiLocalNotificationDateInterpretation:
        UILocalNotificationDateInterpretation.absoluteTime);
    

    此示例将安排在 5 秒后出现的通知。

    【讨论】:

      【解决方案2】:

      经过数小时的研究,我已经解决了这个问题。 完整代码如下。

      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);
      }
      

      像这样添加您的自定义时间

      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',
        );
      }
      

      【讨论】:

        【解决方案3】:

        试试Awesome Notifications
        它具有许多功能,包括秒精度的预定通知。

        【讨论】:

          【解决方案4】:

          抱歉,我是新手,还没有足够的声誉来发表评论,所以它在后台工作吗?

          【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2022-12-16
          • 2022-06-26
          • 1970-01-01
          • 2017-09-10
          • 2021-10-13
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多