我创建了一个功能,可以在您设置的特定时间和一周中的某一天发出闹钟。
func scheduleNotification() {
let center = UNUserNotificationCenter.current()
let hour = 6
let minute = 40
let weekdays = [2,3,4] // mon, tue, wed
let content = UNMutableNotificationContent()
content.title = "Fire!!"
content.body = "test.!"
content.badge = 1
content.categoryIdentifier = "alarm"
content.userInfo = ["customData": "fizzbuzz"]
content.sound = UNNotificationSound.default
for weekday in weekdays {
var dateComponents = DateComponents()
dateComponents.hour = hour
dateComponents.minute = minute
dateComponents.weekday = weekday
let trigger = UNCalendarNotificationTrigger.init(dateMatching: dateComponents, repeats: true)
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
center.add(request)
center.getPendingNotificationRequests { (requests) in
for request in requests {
print("trigger : (request.trigger)")
}
}
UNUserNotificationCenter.current().add(request) { (error) in
}
}
}
正如您在上面的代码中看到的,您要重复的日子是星期一、星期二和星期三。
weekdays = [2,3,4]
之后,我想使用 [2,3,4] 数组上的 for 语句创建一个在星期一、星期二和星期三响起的闹钟。
但是闹钟并没有像我想要的那样响起。
我使用 getPendingNotificationRequests 方法将计划警报列表打印到控制台。
trigger : Optional(<UNCalendarNotificationTrigger: 0x283242f20; dateComponents: <NSDateComponents: 0x2830567a0> {
Hour: 6
Minute: 40
Weekday: 2, repeats: YES>)
trigger : Optional(<UNCalendarNotificationTrigger: 0x283241920; dateComponents: <NSDateComponents: 0x283056900> {
Hour: 6
Minute: 40
Weekday: 2, repeats: YES>)
trigger : Optional(<UNCalendarNotificationTrigger: 0x2832416a0; dateComponents: <NSDateComponents: 0x2830556f0> {
Hour: 6
Minute: 40
Weekday: 3, repeats: YES>)
trigger : Optional(<UNCalendarNotificationTrigger: 0x283242f20; dateComponents: <NSDateComponents: 0x283056900> {
Hour: 6
Minute: 40
Weekday: 2, repeats: YES>)
trigger : Optional(<UNCalendarNotificationTrigger: 0x2832426e0; dateComponents: <NSDateComponents: 0x283054c40> {
Hour: 6
Minute: 40
Weekday: 3, repeats: YES>)
trigger : Optional(<UNCalendarNotificationTrigger: 0x2832413c0; dateComponents: <NSDateComponents: 0x283056930> {
Hour: 6
Minute: 40
Weekday: 4, repeats: YES>)
我想要的是三个触发器,Weekday 一个接一个地存储在 [2,3,4] 数组中。
但是,创建了六个触发器,并且工作日的顺序存储不正确。
我想问两个问题。
如果你看一下上面代码的日志,如果你有一系列触发器,Weekday 存储为 2,它们是否一个接一个地执行?例如,如果是星期一,今天响一次,下周一响一次?
为什么警报存储如此错误,我该如何解决这个问题?
你得到六个触发器的原因是,在你的 for 循环 (for weekday in weekdays
(,你在每个触发器中添加两次。每个触发器在行center.add(request)
处添加一次,在行UNUserNotificationCenter.current().add(request) { (error) in
处再次添加。您可以通过将 for 循环更新为如下所示来解决此问题:
for weekday in weekdays {
var dateComponents = DateComponents()
dateComponents.hour = hour
dateComponents.minute = minute
dateComponents.weekday = weekday
let trigger = UNCalendarNotificationTrigger.init(dateMatching: dateComponents, repeats: true)
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
center.current().add(request) { (error) in
if error != nil {
print(error.localizedDescription)
}
}
}
您可能还只想在 for 循环完成后打印出触发器,而不是在 for 循环中打印。所以我会在 for 循环之后添加以下代码:
func scheduleNotification() {
...
for weekday in weekdays {
...
}
//This following code was originally within your for loop, but should probably execute after the for loop instead
//The console should look a lot cleaner after doing so
center.getPendingNotificationRequests { (requests) in
for request in requests {
print("trigger : (request.trigger)")
}
}
}