【发布时间】:2017-09-12 01:38:41
【问题描述】:
背景:
我正在编写一个机器人向您发送消息的应用程序。这些消息可以作为本地通知接收。
问题:
当机器人在短时间内(每条消息间隔 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)")
}
}
}
【问题讨论】:
-
正如 Arnav 所说,这是预期的功能,所以现在的问题是,什么是好的解决方法?
-
问题是我试图从应用程序发送通知,即使应用程序已变为非活动状态。通过意识到这一点并在应用程序实际失去活动状态之前排队所有即将到来的通知来解决此问题
标签: ios swift unusernotificationcenter