背景:
我正在编写一个机器人向您发送消息的应用程序。这些消息可以作为本地通知接收。
问题:
当机器人在很短的时间内发送多个通知时(每封邮件之间间隔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)")
}
}
}
答案 0 :(得分:1)
您的代码没有错:
就像您在问题中写的那样,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.