【发布时间】:2020-05-08 14:46:13
【问题描述】:
所以我问了一个类似的问题,但这超出了该问题的范围。我想为用户提供自定义通知何时开始和结束的选项;然后他们会选择通知的间隔。
上一个问题:how to set local notifications between 8am and 8pm every day
这是我现在使用的功能,由@Robert Crabtree 提供:
let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.removeAllDeliveredNotifications()
notificationCenter.removeAllPendingNotificationRequests()
let startHour = 7
let endHour = 23
let totalHours = endHour - startHour
let totalNotifications = totalHours * 2
for i in 0...totalNotifications {
var date = DateComponents()
date.hour = startHour + i / 2
date.minute = 30 * (i % 2)
print("setting reminder for \(date.hour!):\(date.minute!)")
let notification = UNMutableNotificationContent()
notification.title = reminderMessages[randomInt].title
notification.body = reminderMessages[randomInt].body
let trigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: true)
let uuidString = UUID().uuidString
let request = UNNotificationRequest(identifier: uuidString, content: notification, trigger: trigger)
notificationCenter.add(request, withCompletionHandler: nil)
}
【问题讨论】:
标签: swift notifications intervals unusernotificationcenter