[英]Is there a way to skip a recurring notification using flutter_local_notifications?
I'm using flutter_local_notifications to schedule daily reminder notifications but I want to prevent users from being bothered by the notification if they already accomplished the action that day.我正在使用flutter_local_notifications来安排每日提醒通知,但我想防止用户在当天已经完成操作的情况下被通知打扰。 In other words, I want to be able to skip a day.
换句话说,我希望能够跳过一天。
Since there is no way with FlutterLocalNotificationsPlugin
to run some code before triggering each recurring notification, I thought I could do the following workaround when needing to skip one day: cancel the recurring notification and reschedule it with a TZDateTime
parameter set to the next day .由于
FlutterLocalNotificationsPlugin
无法在触发每个重复通知之前运行一些代码,我想我可以在需要跳过一天时执行以下解决方法:取消重复通知并使用设置为第二天的TZDateTime
参数重新安排它。 Unfortunately, it doesn't work this way.不幸的是,它不是这样工作的。
The doc clearly says ( source ):文档清楚地说( 来源):
Note that when a value is given, the [scheduledDate] may not represent the first time the notification will be shown.
请注意,当给定值时,[scheduledDate] 可能不代表第一次显示通知。 An example would be if the date and time is currently 2020-10-19 11:00 (ie 19th October 2020 11:00AM) and [scheduledDate] is 2020-10-21 10:00 and the value of the [matchDateTimeComponents] is [DateTimeComponents.time], then the next time a notification will appear is 2020-10-20 10:00.
例如,如果日期和时间当前是 2020-10-19 11:00(即 2020 年 10 月 19 日上午 11:00)并且 [scheduledDate] 是 2020-10-21 10:00 并且 [matchDateTimeComponents] 的值是[DateTimeComponents.time],那么下一次出现通知的时间是2020-10-20 10:00。
I'm afraid the only solution at this point is to manage my own background tasks using something like workmanager instead of FlutterLocalNotificationsPlugin
.恐怕此时唯一的解决方案是使用workmanager之类的东西而不是
FlutterLocalNotificationsPlugin
来管理我自己的后台任务。 Am I right?我对吗? Any other suggestions?
还有其他建议吗?
I encountered the same problem.我遇到了同样的问题。 The solution I'm going with is manually scheduling all the periodic notifications for X time into the future (say one month) with zonedSchedule (not configuring it to repeat).
我要使用的解决方案是使用zonedSchedule (不将其配置为重复)手动安排 X 时间的所有定期通知到未来(比如一个月)。
Next, you can cancel any of the notifications you want with cancel .接下来,你可以取消任何你想要的通知取消。 Then, you just have to make sure to reschedule a month into the future of daily notifications when it gets close to the initial scheduling time.
然后,您只需确保在接近初始安排时间时将每日通知的未来重新安排为一个月。 You can use shared preferences to keep track of the limit of the scheduling to check whenever the app is opened for example.
例如,您可以使用共享首选项来跟踪计划的限制,以便在打开应用程序时进行检查。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.