如何显示多个本地通知

时间:2017-09-12 01:38:41

标签: ios swift unusernotificationcenter

背景:

我正在编写一个机器人向您发送消息的应用程序。这些消息可以作为本地通知接收。

问题:

当机器人在很短的时间内发送多个通知时(每封邮件之间间隔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)")
        }
    }
}

1 个答案:

答案 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.

https://developer.apple.com/library/content/technotes/tn2265/_index.html#//apple_ref/doc/uid/DTS40010376-CH1-TNTAG23