iOS 本地通知队列如何为 UNTimeIntervalNotificationTrigger 工作?
How does iOS Local Notification Queueing work for UNTimeIntervalNotificationTrigger?
如果我创建一个简单的应用程序,我希望在连续 4 分钟内每分钟发送一个通知;在最初的 5 秒延迟之后。
当我调用下面的 scheduleManyNotes() 时,我打印出待处理的通知并且只看到 1 个。是什么原因导致这些通知被组合到 1 个中?
func scheduleManyNotes() {
for x in 0...4 {
scheduleNote("note \(x)", (x * 60) + 5)
}
notificationCenter.getPendingNotificationRequests(completionHandler:{reqs in
for request in reqs {
print(request)
}
})
}
func scheduleNote(_ msg: String, _ delaySec: Int) {
let content = UNMutableNotificationContent()
content.sound = UNNotificationSound.default
content.body = msg
content.badge = NSNumber(integerLiteral: delaySec)
content.categoryIdentifier = msg
let trigger = delaySec == 0 ? nil : UNTimeIntervalNotificationTrigger(timeInterval: Double(delaySec), repeats: false)
let request = UNNotificationRequest(identifier: "identifier", content: content, trigger: trigger)
NSLog("Scheduling Request \(msg)")
notificationCenter.add(request) { (error) in
if let error = error {
NSLog("Error \(error.localizedDescription)")
}
}
}
问题是我对所有 TimeInterval 通知使用了相同的标识符。一个我将标识符更改为对每个请求都是唯一的,然后我有 5 个唯一请求。
// Original
let request = UNNotificationRequest(identifier: "identifier", content: content, trigger: trigger)
// Modfified
let uid = UUID.init().uuidString
print("hopefully unique uuid:\(uid)")
let request = UNNotificationRequest(identifier: uid, content: content, trigger: trigger)
如果我创建一个简单的应用程序,我希望在连续 4 分钟内每分钟发送一个通知;在最初的 5 秒延迟之后。
当我调用下面的 scheduleManyNotes() 时,我打印出待处理的通知并且只看到 1 个。是什么原因导致这些通知被组合到 1 个中?
func scheduleManyNotes() {
for x in 0...4 {
scheduleNote("note \(x)", (x * 60) + 5)
}
notificationCenter.getPendingNotificationRequests(completionHandler:{reqs in
for request in reqs {
print(request)
}
})
}
func scheduleNote(_ msg: String, _ delaySec: Int) {
let content = UNMutableNotificationContent()
content.sound = UNNotificationSound.default
content.body = msg
content.badge = NSNumber(integerLiteral: delaySec)
content.categoryIdentifier = msg
let trigger = delaySec == 0 ? nil : UNTimeIntervalNotificationTrigger(timeInterval: Double(delaySec), repeats: false)
let request = UNNotificationRequest(identifier: "identifier", content: content, trigger: trigger)
NSLog("Scheduling Request \(msg)")
notificationCenter.add(request) { (error) in
if let error = error {
NSLog("Error \(error.localizedDescription)")
}
}
}
问题是我对所有 TimeInterval 通知使用了相同的标识符。一个我将标识符更改为对每个请求都是唯一的,然后我有 5 个唯一请求。
// Original
let request = UNNotificationRequest(identifier: "identifier", content: content, trigger: trigger)
// Modfified
let uid = UUID.init().uuidString
print("hopefully unique uuid:\(uid)")
let request = UNNotificationRequest(identifier: uid, content: content, trigger: trigger)