重新安排 Flutter 时,本地通知不会在新时间触发



在我的应用程序中,我使用flutter_local_notifications: ^1.4.2插件进行本地通知。用户选择要安排alarm的工作日,并且在保存时,我将alarm对象传递给setSchedulerNotifications方法,以在警报的repeatWeekdays列表中设置每天的通知。我循环查看repeatWeekdays列表,并在每个循环中设置一个通知,其id为传入的警报id+循环编号,为增量。到目前为止,一切如预期。

现在,当警报被编辑时(不同的时间(,在保存时,我将新警报传递给modifySchedulerNotification方法,该方法首先调用deleteSchedulerNotifications方法(具有相同的循环来计算通知id(,然后再次调用setSchedulerNotifications

打印显示setSchedulerNotifications中的通知ID是正确的,并且在deleteSchedulerNotifications和第二个setSchedulerNotifications调用中是一致的,但当设置第二次(与第一次相同的ID(时,通知不会在新时间激发,而是在旧时间激发。

你能发现我做错了什么吗?

通知方式:

Set

Future<void> setSchedulerNotifications(Alarm alarm) async {
print(
'##### LocalNotificationRepository().setSchedulerNotifications called');
print('##### sound to play is : ${alarm.sound.toLowerCase()}');
for (int i = 0; i < alarm.repeatWeekdays.length; i++) {
List<String> repeat = alarm.repeatWeekdays;
int notificationId = int.parse(alarm.alarmId) + i;
print('##### Scheduling local notification with id: $notificationId,n'
'###### for alarm with id ${alarm.alarmId}n'
'##### i = $i');
String notificationChannelId = 'checkRoute_${alarm.sound}';
String notificationChannelName = 'checkRoute_${alarm.alarmName}';
String notificationChannelDescription =
'Scherduled checking for route ${alarm.alarmName}';
String sound = alarm.sound.toLowerCase();
DateFormat dateFormat = DateFormat('HH:mm');
var date = dateFormat.parse(alarm.time);
int hour = date.hour;
int minute = date.minute;
Time time = Time(hour, minute, 0);
var androidPlatformChannelSpecifics = AndroidNotificationDetails(
notificationChannelId, //'show weekly channel id',
notificationChannelName, //'show weekly channel name',
notificationChannelDescription, //'show weekly channel description');
priority: Priority.High,
sound: RawResourceAndroidNotificationSound('$sound'));
var iOSPlatformChannelSpecifics = IOSNotificationDetails(
presentAlert: true, presentSound: true, presentBadge: true);
var platformChannelSpecifics = NotificationDetails(
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin
.showWeeklyAtDayAndTime(
notificationId,
sprintf(
AppLocalizations.instance
.text('ROUTE_CHECK_NOTIFICATION_TITLE'),
[alarm.alarmName]), //'show weekly title',
AppLocalizations.instance.text(
'ROUTE_CHECK_NOTIFICATION_BODY'), //'show weekly subtitle',
selectDay(repeat[i]),
time,
platformChannelSpecifics,
//          categoryIdentifier: "ROUTE_CHECK_CATEGORY",
payload: alarm.routeName)
.whenComplete(() {
print('##### Scheduled local notification with id: $notificationId');
});
}
print('##### LocalNotificationRepository().setWeeklyNotifications()n'
'##### all notifications for this route check are set');
}

删除

Future<void> deleteSchedulerNotifications(Alarm alarm) async {
print(
'@@@@ LocalNotificationRepository().deleteSchedulerNotifications called');
for (int i = 0; i < alarm.repeatWeekdays.length; i++) {
int id = int.parse(alarm.alarmId) + i;
print('@@@@ Deleting local notification with id: $id,n'
'###### for alarm with id ${alarm.alarmId}n'
'##### i = $i');
await flutterLocalNotificationsPlugin.cancel(id).catchError((e) {
print(
'@@@@ LocalNotificationRepository().deleteSchedulerNotifications error: $e');
}).whenComplete(() {
print('@@@@ Deleted local notification with id: $id');
print('@@ LocalNotificationRepository().deleteWeeklyNotifications()n'
'@@ all notifications for this route check are deleted');
//        return;
});
}
}

修改

Future<void> modifySchedulerNotification(Alarm alarm) async {
print(
'## LocalNotificationRepository().modifyWeeklyNotifications() calledn'
'## for alarm ${alarm.toMap().toString()}');
await deleteSchedulerNotifications(alarm).whenComplete(() async {
print('## LocalNotificationRepository().modifyWeeklyNotifications()n'
'## old notifications for alarm ${alarm.toMap().toString()} have been deleted.n'
'## are there new notifications to be set? ${alarm.enabled}');
if (alarm.enabled == true) {
await setSchedulerNotifications(alarm).whenComplete(() {
print('## LocalNotificationRepository().modifyWeeklyNotifications()n'
'## new notifications for alarm ${alarm.toMap().toString()} are set');
return;
});
}
});
print(
'modifyWeeklyNotifications() all notifications for this route check are modified');
}

打印:

第一次保存

I/flutter ( 1520): SetNotification received
I/flutter ( 1520): SchedulerRepository().saveAlarm() calledfor alarm: {alarmId: 815816479, alarmName: test, enabled: true, repeatWeekdays: ["Monday","Tuesday","Wednesday","Thursday","Friday"], time: 21:05, sound: fixitTheme, routeName: test}
I/flutter ( 1520): ##### LocalNotificationRepository().setSchedulerNotifications called
I/flutter ( 1520): ##### sound to play is : fixittheme
I/flutter ( 1520): ##### Scheduling local notification with id: 815816479,
I/flutter ( 1520): ###### for alarm with id 815816479
I/flutter ( 1520): ##### i = 0
I/flutter ( 1520): ##### Scheduled local notification with id: 815816479
I/flutter ( 1520): ##### Scheduling local notification with id: 815816480,
I/flutter ( 1520): ###### for alarm with id 815816479
I/flutter ( 1520): ##### i = 1
I/flutter ( 1520): ##### Scheduled local notification with id: 815816480
I/flutter ( 1520): ##### Scheduling local notification with id: 815816481,
I/flutter ( 1520): ###### for alarm with id 815816479
I/flutter ( 1520): ##### i = 2
I/flutter ( 1520): ##### Scheduled local notification with id: 815816481
I/flutter ( 1520): ##### Scheduling local notification with id: 815816482,
I/flutter ( 1520): ###### for alarm with id 815816479
I/flutter ( 1520): ##### i = 3
I/flutter ( 1520): ##### Scheduled local notification with id: 815816482
I/flutter ( 1520): ##### Scheduling local notification with id: 815816483,
I/flutter ( 1520): ###### for alarm with id 815816479
I/flutter ( 1520): ##### i = 4
I/flutter ( 1520): ##### Scheduled local notification with id: 815816483
I/flutter ( 1520): ##### LocalNotificationRepository().setWeeklyNotifications()
I/flutter ( 1520): ##### all notifications for this route check are set

修改保存

I/flutter ( 1520): ModifyNotification received
I/flutter ( 1520): SchedulerRepository().saveAlarm() calledfor alarm: {alarmId: 815816479, alarmName: test, enabled: true, repeatWeekdays: ["Monday","Tuesday","Wednesday","Thursday","Friday"], time: 21:07, sound: fixitTheme, routeName: test}
I/flutter ( 1520): ## LocalNotificationRepository().modifyWeeklyNotifications() called
I/flutter ( 1520): ## for alarm {alarmId: 815816479, alarmName: test, enabled: true, repeatWeekdays: ["Monday","Tuesday","Wednesday","Thursday","Friday"], time: 21:05, sound: fixitTheme, routeName: test}
I/flutter ( 1520): @@@@ LocalNotificationRepository().deleteSchedulerNotifications called
I/flutter ( 1520): @@@@ Deleting local notification with id: 815816479,
I/flutter ( 1520): ###### for alarm with id 815816479
I/flutter ( 1520): ##### i = 0
I/flutter ( 1520): @@@@ Deleted local notification with id: 815816479
I/flutter ( 1520): @@ LocalNotificationRepository().deleteWeeklyNotifications()
I/flutter ( 1520): @@ all notifications for this route check are deleted
I/flutter ( 1520): @@@@ Deleting local notification with id: 815816480,
I/flutter ( 1520): ###### for alarm with id 815816479
I/flutter ( 1520): ##### i = 1
I/flutter ( 1520): @@@@ Deleted local notification with id: 815816480
I/flutter ( 1520): @@ LocalNotificationRepository().deleteWeeklyNotifications()
I/flutter ( 1520): @@ all notifications for this route check are deleted
I/flutter ( 1520): @@@@ Deleting local notification with id: 815816481,
I/flutter ( 1520): ###### for alarm with id 815816479
I/flutter ( 1520): ##### i = 2
I/flutter ( 1520): @@@@ Deleted local notification with id: 815816481
I/flutter ( 1520): @@ LocalNotificationRepository().deleteWeeklyNotifications()
I/flutter ( 1520): @@ all notifications for this route check are deleted
I/flutter ( 1520): @@@@ Deleting local notification with id: 815816482,
I/flutter ( 1520): ###### for alarm with id 815816479
I/flutter ( 1520): ##### i = 3
I/flutter ( 1520): @@@@ Deleted local notification with id: 815816482
I/flutter ( 1520): @@ LocalNotificationRepository().deleteWeeklyNotifications()
I/flutter ( 1520): @@ all notifications for this route check are deleted
I/flutter ( 1520): @@@@ Deleting local notification with id: 815816483,
I/flutter ( 1520): ###### for alarm with id 815816479
I/flutter ( 1520): ##### i = 4
I/flutter ( 1520): @@@@ Deleted local notification with id: 815816483
I/flutter ( 1520): @@ LocalNotificationRepository().deleteWeeklyNotifications()
I/flutter ( 1520): @@ all notifications for this route check are deleted
I/flutter ( 1520): ## LocalNotificationRepository().modifyWeeklyNotifications()
I/flutter ( 1520): ## old notifications for alarm {alarmId: 815816479, alarmName: test, enabled: true, repeatWeekdays: ["Monday","Tuesday","Wednesday","Thursday","Friday"], time: 21:05, sound: fixitTheme, routeName: test} have been deleted.
I/flutter ( 1520): ## are there new notifications to be set? true
I/flutter ( 1520): ##### LocalNotificationRepository().setSchedulerNotifications called
I/flutter ( 1520): ##### sound to play is : fixittheme
I/flutter ( 1520): ##### Scheduling local notification with id: 815816479,
I/flutter ( 1520): ###### for alarm with id 815816479
I/flutter ( 1520): ##### i = 0
I/flutter ( 1520): ##### Scheduled local notification with id: 815816479
I/flutter ( 1520): ##### Scheduling local notification with id: 815816480,
I/flutter ( 1520): ###### for alarm with id 815816479
I/flutter ( 1520): ##### i = 1
I/flutter ( 1520): ##### Scheduled local notification with id: 815816480
I/flutter ( 1520): ##### Scheduling local notification with id: 815816481,
I/flutter ( 1520): ###### for alarm with id 815816479
I/flutter ( 1520): ##### i = 2
I/flutter ( 1520): ##### Scheduled local notification with id: 815816481
I/flutter ( 1520): ##### Scheduling local notification with id: 815816482,
I/flutter ( 1520): ###### for alarm with id 815816479
I/flutter ( 1520): ##### i = 3
I/flutter ( 1520): ##### Scheduled local notification with id: 815816482
I/flutter ( 1520): ##### Scheduling local notification with id: 815816483,
I/flutter ( 1520): ###### for alarm with id 815816479
I/flutter ( 1520): ##### i = 4
I/flutter ( 1520): ##### Scheduled local notification with id: 815816483
I/flutter ( 1520): ##### LocalNotificationRepository().setWeeklyNotifications()
I/flutter ( 1520): ##### all notifications for this route check are set
I/flutter ( 1520): ## LocalNotificationRepository().modifyWeeklyNotifications()
I/flutter ( 1520): ## new notifications for alarm {alarmId: 815816479, alarmName: test, enabled: true, repeatWeekdays: ["Monday","Tuesday","Wednesday","Thursday","Friday"], time: 21:05, sound: fixitTheme, routeName: test} are set
I/flutter ( 1520): modifyWeeklyNotifications() all notifications for this route check are modified
I/flutter ( 1520): LocalNotificationBLoc._modifySchedulerNotification() notifications have been modified

终于找到了错误。。我正在将旧的Alarm对象传递给modifySchedulerNotification方法。

相关内容

最新更新