【发布时间】:2021-12-17 12:04:09
【问题描述】:
我试图将本地通知设置为在每个月的最后一天触发,无论是 28 日、29 日、30 日还是 31 日。
使用之前的框架UILocalNotification,我可以指定fireDate 和repeatInterval,它可以工作。但是现在我将框架更新为UNNotofication,我尝试使用UNCalendarNotificationTrigger 并将DateComponents' 日期设置为0。然而并没有火。有什么我做错了吗?谢谢!
代码如下。 nextTriggerDate 为零。
var components = DateComponents()
components.hour = 8
components.minute = 0
components.day = 0
let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: true)
print(trigger.nextTriggerDate())
【问题讨论】:
-
您的结果是
nil,因为您将day设置为0。 -
@shim,你是对的,但是它之前使用 UILocalNotification 工作,将日期设置为 0 它会给我上个月的最后一天。
Calendar.current.date(from: components)实际上给你 12 月 31 日。问题是这样 dateComponents 不能与 UNCalendarNotificationTrigger 一起使用 -
请出示您的原始代码进行比较
标签: ios swift unnotificationrequest unnotificationtrigger