admin管理员组文章数量:1294661
I'm using Flutter Local Notifications to send a reminder to users every day at a time of their choice. My schedule function in my notification service looks like this:
Future<void> scheduleDailyNotification(
DateTime setTime, String title, String body, String payload) async {
// Setup notification details
const DarwinNotificationDetails iosNotificationDetails =
DarwinNotificationDetails(presentAlert: true, presentSound: true);
const NotificationDetails notificationDetails =
NotificationDetails(iOS: iosNotificationDetails);
// Convert to TZDateTime
final tz.TZDateTime scheduledDate = tz.TZDateTime(
tz.local,
setTime.year,
setTime.month,
setTime.day,
setTime.hour,
setTime.minute,
setTime.second,
);
logger.d('Scheduling notification for: $scheduledDate');
// Schedule notification
await _flutterLocalNotificationsPlugin.zonedSchedule(
0, title, body, scheduledDate, notificationDetails,
payload: payload,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime,
matchDateTimeComponents: DateTimeComponents.time);
}
Now this reminder should only be sent if the user did not take a specific action in the app. If the user takes this action, I want to cancel todays notification, but start sending out notifications again tomorrow:
Future<void> cancelTodaysNotification() async {
final tz.TZDateTime now = tz.TZDateTime.now(tz.local);
DateTime setTime = await _getNotificationsTimeFromPrefs();
final tz.TZDateTime scheduledDate = tz.TZDateTime(
tz.local, now.year, now.month, now.day, setTime.hour, setTime.minute);
if (now.isBefore(scheduledDate)) {
logger.d('Cancelling notification for today');
await _flutterLocalNotificationsPlugin.cancel(0);
// Get next valid date and add one day
final tomorrow =
_nextInstanceOfTime(setTime).add(const Duration(days: 1));
await scheduleDailyNotification(tomorrow, _notificationTitle,
_notificationBody, _notificationPayload);
}
}
This does not work because matchDateTimeComponents: DateTimeComponents.time
means that the date is ignored. It will just schedule a notification for today again (given that the time is in the future)
Is there a way to cancel today's notification without adding a background process to the app?
One idea I had is to use DateTimeComponents.dayOfWeekAndTime
instead of DateTimeComponents.time
. If today is Wednesday I could cancel all notifications and then schedule them again for every day except Wednesday. BUT this would mean that from now on no notifications will be sent on Wednesdays...
This seems like a very obvious feature and I hope I'm missing something.
本文标签: iosHow to skip one recurring notification with flutter local notificationsStack Overflow
版权声明:本文标题:ios - How to skip one recurring notification with flutter local notifications? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741604113a2387870.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论