[英]How to show multiple local notifications
背景:
我正在编写一个机器人向您发送消息的应用程序。 这些消息可以作为本地通知接收。
问题:
当漫游器在短时间内(每条消息之间间隔1秒)发送多个通知时,通知中心将仅显示一条消息。 我每次都会听到通知声音,但是我仍然只会看到第一条消息。
相关代码:
func postUserNotification(content: String, delay: TimeInterval, withDictionary dictionary: [String:String] = [:]) {
let notificationContent = UNMutableNotificationContent()
notificationContent.body = content
notificationContent.userInfo = dictionary
notificationContent.categoryIdentifier = "message"
let dateAfterDelay = Date(timeIntervalSinceNow: delay)
let dateComponents = Calendar.current.dateComponents([.year,.month,.day,.hour,.minute,.second], from: dateAfterDelay)
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)
let identifier = "identifier" + "\(NotificationManager.incrementor)"
let localNotification = UNNotificationRequest(identifier: identifier, content: notificationContent, trigger: trigger)
UNUserNotificationCenter.current().add(localNotification){ (error : Error?) in
if let theError = error {
print("the error is \(theError.localizedDescription)")
}
}
}
您的代码没有错:
就像您在问题中所写的一样,Apple Docs也提到了这一点:
If you are sending multiple notifications to the same device or
computer within a short period of time, the push service will send only
the last one.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.