首页 文章

如何显示多个本地通知

提问于
浏览
0

背景:

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

问题:

当机器人在很短的时间内发送多个通知时(每条消息之间间隔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 回答

相关问题