迅速设置特定日期和时间的本地通知3
问题描述:
在我的应用程序中,我想添加本地通知。该场景将是用户可以选择从Mon到Sun的任何时间和日期。例如,如果用户选择星期一,星期四和星期六为下午11点的日期和时间,那么现在应在所有选定日期和特定时间通知用户。迅速设置特定日期和时间的本地通知3
代码:
let notification = UNMutableNotificationContent()
notification.title = "Danger Will Robinson"
notification.subtitle = "Something This Way Comes"
notification.body = "I need to tell you something, but first read this."
let notificationTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: true)
// let test = UNCalendarNotificationTrigger()
let request = UNNotificationRequest(identifier: "notification1", content: notification, trigger: notificationTrigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
我使用此代码,但这并不根据我所需要的工作。
答
要获得在某个时间在某个工作日的重复本地通知您可以使用UNCalendarNotificationTrigger
:
let notification = UNMutableNotificationContent()
notification.title = "Danger Will Robinson"
notification.subtitle = "Something This Way Comes"
notification.body = "I need to tell you something, but first read this."
// add notification for Mondays at 11:00 a.m.
var dateComponents = DateComponents()
dateComponents.weekday = 2
dateComponents.hour = 11
dateComponents.minute = 0
let notificationTrigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
let request = UNNotificationRequest(identifier: "notification1", content: notification, trigger: notificationTrigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
如果您想在11:00在周一,周四和周六通知您需要添加3分开请求。为了能够删除它们,你必须跟踪标识符。
您在此代码中的timeInterval是60秒。所以,它会在60秒后开火。你没有实施任何在多天内触发它的逻辑? –
类似于https://stackoverflow.com/questions/41800189/local-notifications-repeat-interval-in-swift-3看起来很有趣 – MadProgrammer