Ios 设置特定日期和时间的本地通知swift 3

Ios 设置特定日期和时间的本地通知swift 3,ios,swift3,uilocalnotification,Ios,Swift3,Uilocalnotification,在我的应用程序中,我想添加本地通知。这种情况下,用户可以选择从周一到周日的任何时间和天数。例如,如果用户选择周一、周四和周六为天,时间为晚上11:00,那么现在应该在所有选择的天和特定时间通知用户 代码: 我正在使用此代码,但这不符合我的需要。要获取在某个工作日、某个时间重复的本地通知,可以使用UNCalendarNotificationTrigger: let notification = UNMutableNotificationContent() notification.title =

在我的应用程序中,我想添加本地通知。这种情况下,用户可以选择从周一到周日的任何时间和天数。例如,如果用户选择周一、周四和周六为天,时间为晚上11:00,那么现在应该在所有选择的天和特定时间通知用户

代码:


我正在使用此代码,但这不符合我的需要。

要获取在某个工作日、某个时间重复的本地通知,可以使用
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个单独的请求。要删除它们,您必须跟踪标识符。

此代码中的时间间隔为60秒。所以,它会在60秒后开火。你没有在几天内实现任何触发它的逻辑?看起来很有趣的回答!谢谢我有一个问题,您知道我如何在特定时间停止重复此通知吗?@MohamedEzzat您可以设置通知请求的标识符属性,并随时使用该标识符将其从通知中心删除。
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)