问题描述
我使用的是 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.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,_temp,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.
请帮我解决这个问题。任何帮助将不胜感激。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)