我可以在一个时间间隔内设置一个通知,但我不知道如何在特定的时间和日期内发出通知,我尝试了这个,但不工作。
let center = UNUserNotificationCenter.current()
func notificationSender(){
center.requestAuthorization([.sound, .alert]) {
(granted, error) in
// We can register for remote notifications here too!
}
var date = DateComponents()
date.hour = 13
date.minute = 57
let trigger = UNCalendarNotificationTrigger.init(dateMatching: date , repeats: false)
let content = UNNotificationContent()
// edit your content
let notification = UNNotificationRequest(identifier: "myNotification", content: content, trigger: trigger)Center.add(通知)}
通知须于每星期一及五下午三时重播。
发布于 2016-10-15 14:39:23
你快到了。你没什么可做的了。
您所缺少的内容如下:
weekday添加到日期组件中,1用于星期日,因此在您的情况下,您需要将其设置为周一通知的2和周五通知的6。repeats设置为UNCalendarNotificationTrigger中的true。下面是一个例子。
// Create date components that will match each Monday at 15:00
var dateComponents = DateComponents()
dateComponents.hour = 15
dateComponents.minute = 0
dateComponents.weekday = 2 // Monday
// Create a calendar trigger for our date compontents that will repeat
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents,
repeats: true)
// Create the content for our notification
let content = UNMutableNotificationContent()
content.title = "Foobar"
content.body = "You will see this notification each monday at 15:00"
// Create the actual notification
let request = UNNotificationRequest(identifier: "foo",
content: content,
trigger: trigger)
// Add our notification to the notification center
UNUserNotificationCenter.current().add(request)https://stackoverflow.com/questions/38252389
复制相似问题