如何显示多个本地通知
How to show multiple local notifications
背景:
我正在编写一个机器人向您发送消息的应用程序。这些消息可以作为本地通知接收。
问题:
当bot在短时间内(每条消息间隔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.
背景:
我正在编写一个机器人向您发送消息的应用程序。这些消息可以作为本地通知接收。
问题:
当bot在短时间内(每条消息间隔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.