zonedSchedule设置远期日期时提前触发通知问题求助
解决zonedSchedule设置数月后通知提前触发的问题
我在使用zonedSchedule设置数月后的通知时,会在设定日期前收到推送通知。以下是我的代码:
scheduleNotification( DateTime date, String title, String description, int id) async { var tzDateNotif = tz.TZDateTime.from(date, tz.local); await _localNotificationService.zonedSchedule( id, title, description, tzDateNotif, const NotificationDetails( android: AndroidNotificationDetails( 'your channel id', 'your channel description')), androidAllowWhileIdle: true, uiLocalNotificationDateInterpretation: UILocalNotificationDateInterpretation.absoluteTime, matchDateTimeComponents: DateTimeComponents.time, ); }
问题原因
你设置的matchDateTimeComponents: DateTimeComponents.time会让通知每天在指定时间重复触发,而非仅在目标日期触发一次,这就是提前收到推送的核心原因。
解决方法
- 若只需在指定日期触发一次通知,移除
matchDateTimeComponents参数,或显式设置为DateTimeComponents.none,这样通知只会在传入的tzDateNotif时间点触发一次。 - 同时确认传入的
date参数是正确的目标日期,没有被错误计算(比如提前了数月)。
修正后的代码
scheduleNotification( DateTime date, String title, String description, int id) async { var tzDateNotif = tz.TZDateTime.from(date, tz.local); await _localNotificationService.zonedSchedule( id, title, description, tzDateNotif, const NotificationDetails( android: AndroidNotificationDetails( 'your channel id', 'your channel description')), androidAllowWhileIdle: true, uiLocalNotificationDateInterpretation: UILocalNotificationDateInterpretation.absoluteTime, matchDateTimeComponents: DateTimeComponents.none, ); }
内容的提问来源于stack exchange,提问作者Moïse Rajesearison
相关产品推荐
相关产品推荐

