0

插入

我正在使用flutter_local_notifications最新版本(6.0.0)

应用程序

我正在开发一个提醒应用程序,用户可以将本地通知设置为每周在选定的星期几显示,每 X 小时重复一次。通知可以有开始时间和结束时间。例如,将通知设置为每周六、周日、周三从 08:30 开始,到 15:30 结束,并在这些时间之间每 2 小时重复一次(8:30、10:30、12:30、14:30) . 为简单起见,我将为一周中的每一天设置通知。

错误

问题是,有时会显示通知,有时会显示通知。第一个通知始终显示(在上面的示例中为 8:30),但有时其他通知不显示。我确实设置了所有通知。

我的规格

我正在使用最新版本的插件(6.0.0)。我的手机是带有 Android 8.0 的 LG V20。Flutter 版本为 2.2.0(本期末尾有flutter doctor)。

代码示例

我的代码有点复杂。我可以提供完整的代码,但很难理解。我会提供我认为需要的部分。我对代码进行了注释,使其易于理解,并使用上面的示例作为提供给该函数的数据:

  void showNotificationWeekly(
      int id, String title, String body, Reminder reminder) async {
        // Initialize Timezone
    tz.initializeTimeZones();
    final String currentTimeZone =
        await FlutterNativeTimezone.getLocalTimezone();
    print("timezone = $currentTimeZone");
    tz.setLocalLocation(tz.getLocation(currentTimeZone));
    // Initialize Timezone done
    int _notificationId = 0;
    List<int> _notificationIdList = []; // I want to set the IDs in the shared Pref, Thats what this list is for
    /// [weekValues] is an array of 7 bools,  0 to 6 representing each day of week
    /// is this loop, I set all the notification for the days of week that user has selected
    /// in our example we have selected all the days of week, so [weekValues] is array of 7 true booleans.
    for (var i = 0; i < reminder.weekValues.length; i++) { 
      tz.TZDateTime _weekDateTime =
          _getNextDateTime(i + 1, reminder.timeValues);
      tz.TZDateTime _timeEnd = tz.TZDateTime(
        tz.local,
        _weekDateTime.year,
        _weekDateTime.month,
        _weekDateTime.day,
        reminder.timeValues[1][0],
        reminder.timeValues[1][1],
      );
      /// since [weekValues] is all true, the else of this if statement is useless here
      if (reminder.weekValues[i]) {
        /// [timeValues[2][0]] is my repeating hour, this part is complicated but in this example
        /// [timeValues[2][0]] is 2 so the if is true
        if (reminder.timeValues[2][0] > 0) {
          while (_weekDateTime.isBefore(_timeEnd)) {
            /// The output of this print is important and will be provided after this code
            print(
                'if Notification with Id= $_notificationId Succesfully Scheduled at $_weekDateTime');
            await flutterLocalNotificationsPlugin.zonedSchedule(
                _notificationId,
                title,
                body,
                // _temp,
                _weekDateTime.add(Duration(seconds: 1)),
                getPlatformChannelSpecfics(),
                androidAllowWhileIdle: true,
                uiLocalNotificationDateInterpretation:
                    UILocalNotificationDateInterpretation.absoluteTime,
                matchDateTimeComponents: DateTimeComponents.dayOfWeekAndTime);
                /// some incrementall stuff
            _notificationIdList.add(_notificationId);
            _notificationId++;
            /// Adding to the times base of the repeating time
            /// in this example and if the first loop we add 2 hours to 8:30, so the result is 10:30
            _weekDateTime = _weekDateTime.add(
              Duration(
                hours: reminder.timeValues[2][0],
              ),
            );
          }
        } else { // This else can be ignored since we selected all the week days
          tz.TZDateTime _temp = tz.TZDateTime(
            tz.local,
            _weekDateTime.year,
            _weekDateTime.month,
            _weekDateTime.day,
            _weekDateTime.hour,
            _weekDateTime.minute,
            _weekDateTime.second,
            _weekDateTime.millisecond,
            _weekDateTime.microsecond,
          );
          print(
              'else Notification with Id= $_notificationId Scheduled at $_temp');
          _notificationIdList.add(_notificationId);
          await flutterLocalNotificationsPlugin.zonedSchedule(
              _notificationId,
              title,
              body,
              // _temp,
              _temp,
              getPlatformChannelSpecfics(),
              androidAllowWhileIdle: true,
              uiLocalNotificationDateInterpretation:
                  UILocalNotificationDateInterpretation.absoluteTime,
              matchDateTimeComponents: DateTimeComponents.dayOfWeekAndTime);
        }
        _notificationId++;
      }
    }
    await setNotificationIds(_notificationIdList);
  }

打印输出:


I/flutter ( 7643): Notification with Id= 0 Succesfuly Scheduled at 2021-05-24 08:30:00.000+0430
I/flutter ( 7643): Notification with Id= 1 Succesfuly Scheduled at 2021-05-24 10:30:00.000+0430
I/flutter ( 7643): Notification with Id= 2 Succesfuly Scheduled at 2021-05-24 12:30:00.000+0430
I/flutter ( 7643): Notification with Id= 3 Succesfuly Scheduled at 2021-05-24 14:30:00.000+0430
I/flutter ( 7643): Notification with Id= 5 Succesfuly Scheduled at 2021-05-25 08:30:00.000+0430
I/flutter ( 7643): Notification with Id= 6 Succesfuly Scheduled at 2021-05-25 10:30:00.000+0430
I/flutter ( 7643): Notification with Id= 7 Succesfuly Scheduled at 2021-05-25 12:30:00.000+0430
I/flutter ( 7643): Notification with Id= 8 Succesfuly Scheduled at 2021-05-25 14:30:00.000+0430
I/flutter ( 7643): Notification with Id= 10 Succesfuly Scheduled at 2021-05-26 08:30:00.000+0430
I/flutter ( 7643): Notification with Id= 11 Succesfuly Scheduled at 2021-05-26 10:30:00.000+0430
I/flutter ( 7643): Notification with Id= 12 Succesfuly Scheduled at 2021-05-26 12:30:00.000+0430
I/flutter ( 7643): Notification with Id= 13 Succesfuly Scheduled at 2021-05-26 14:30:00.000+0430
I/flutter ( 7643): Notification with Id= 15 Succesfuly Scheduled at 2021-05-27 08:30:00.000+0430
I/flutter ( 7643): Notification with Id= 16 Succesfuly Scheduled at 2021-05-27 10:30:00.000+0430
I/flutter ( 7643): Notification with Id= 17 Succesfuly Scheduled at 2021-05-27 12:30:00.000+0430
I/flutter ( 7643): Notification with Id= 18 Succesfuly Scheduled at 2021-05-27 14:30:00.000+0430
I/flutter ( 7643): Notification with Id= 20 Succesfuly Scheduled at 2021-05-28 08:30:00.000+0430
I/flutter ( 7643): Notification with Id= 21 Succesfuly Scheduled at 2021-05-28 10:30:00.000+0430
I/flutter ( 7643): Notification with Id= 22 Succesfuly Scheduled at 2021-05-28 12:30:00.000+0430
I/flutter ( 7643): Notification with Id= 23 Succesfuly Scheduled at 2021-05-28 14:30:00.000+0430
I/flutter ( 7643): Notification with Id= 25 Succesfuly Scheduled at 2021-05-29 08:30:00.000+0430
I/flutter ( 7643): Notification with Id= 26 Succesfuly Scheduled at 2021-05-29 10:30:00.000+0430
I/flutter ( 7643): Notification with Id= 27 Succesfuly Scheduled at 2021-05-29 12:30:00.000+0430
I/flutter ( 7643): Notification with Id= 28 Succesfuly Scheduled at 2021-05-29 14:30:00.000+0430
I/flutter ( 7643): Notification with Id= 30 Succesfuly Scheduled at 2021-05-30 08:30:00.000+0430
I/flutter ( 7643): Notification with Id= 31 Succesfuly Scheduled at 2021-05-30 10:30:00.000+0430
I/flutter ( 7643): Notification with Id= 32 Succesfuly Scheduled at 2021-05-30 12:30:00.000+0430
I/flutter ( 7643): Notification with Id= 33 Succesfuly Scheduled at 2021-05-30 14:30:00.000+0430

扑医生

flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 2.2.0, on Microsoft Windows [Version 10.0.19042.985], locale en-US)
[√] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
[√] Chrome - develop for the web
[!] Android Studio (not installed)
[√] VS Code (version 1.56.2)
[√] Connected device (3 available)

! Doctor found issues in 1 category.

请帮我解决这个问题。任何帮助,将不胜感激。

4

0 回答 0