在 "A" a.m 之间每隔 x 分钟重复一些动作。和 "B" p.m
Repeat some action every x minutes between "A" a.m. and "B" p.m
我如何 运行 例如本地通知?
在 UNUserNotificationCenter 中没有重复功能。
也许使用 NSTimer 或类似的东西?
为什么我的代码没有按预期工作
let hours: [Int] = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24]
for hour in hours {
for minute in stride(from: 0, to: 60, by: 5){
let content = UNMutableNotificationContent()
content.title = "Title"
content.body = "Body"
var dateComponents = DateComponents()
dateComponents.hour = hour
dateComponents.minute = minute
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
let request = UNNotificationRequest(identifier: "timerDone", content: content, trigger: trigger)
let center = UNUserNotificationCenter.current()
center.add(request) { (error : Error?) in
if let theError = error {
print(theError.localizedDescription)
}
}
}
}
有重复功能。
let content = UNMutableNotificationContent()
content.title = NSString.localizedUserNotificationString(forKey:
"Hello!", arguments: nil)
content.body = NSString.localizedUserNotificationString(forKey:
"Hello_message_body", arguments: nil)
// Deliver the notification in five seconds and repeat it
content.sound = UNNotificationSound.default()
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60,
repeats: true)
// Schedule the notification.
let request = UNNotificationRequest(identifier: "60_seconds", content: content, trigger: trigger)
let center = UNUserNotificationCenter.current()
center.add(request, withCompletionHandler: nil)
编辑:
正如文档中所写,您当然必须拥有 post 通知的用户权限:
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in
// Enable or disable features based on authorization
}
结果:
通知每分钟 post 次:
我如何 运行 例如本地通知? 在 UNUserNotificationCenter 中没有重复功能。 也许使用 NSTimer 或类似的东西?
为什么我的代码没有按预期工作
let hours: [Int] = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24]
for hour in hours {
for minute in stride(from: 0, to: 60, by: 5){
let content = UNMutableNotificationContent()
content.title = "Title"
content.body = "Body"
var dateComponents = DateComponents()
dateComponents.hour = hour
dateComponents.minute = minute
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
let request = UNNotificationRequest(identifier: "timerDone", content: content, trigger: trigger)
let center = UNUserNotificationCenter.current()
center.add(request) { (error : Error?) in
if let theError = error {
print(theError.localizedDescription)
}
}
}
}
有重复功能。
let content = UNMutableNotificationContent()
content.title = NSString.localizedUserNotificationString(forKey:
"Hello!", arguments: nil)
content.body = NSString.localizedUserNotificationString(forKey:
"Hello_message_body", arguments: nil)
// Deliver the notification in five seconds and repeat it
content.sound = UNNotificationSound.default()
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60,
repeats: true)
// Schedule the notification.
let request = UNNotificationRequest(identifier: "60_seconds", content: content, trigger: trigger)
let center = UNUserNotificationCenter.current()
center.add(request, withCompletionHandler: nil)
编辑:
正如文档中所写,您当然必须拥有 post 通知的用户权限:
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in
// Enable or disable features based on authorization
}
结果:
通知每分钟 post 次: