gpt4 book ai didi

ios - 如何显示多个本地通知

转载 作者:行者123 更新时间:2023-11-28 15:20:00 25 4
gpt4 key购买 nike

背景:

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

问题:

当机器人在短时间内(每条消息间隔 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 文档中提到了这一点:

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

关于ios - 如何显示多个本地通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46166622/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com