简体   繁体   English

Swift 通知相互替换

[英]Swift Notifications replacing each other

I'm trying to make notifications appear when on background mode, but these had to stack on top of each other, or at least all appear on the screen.我试图在后台模式下显示通知,但这些通知必须相互叠加,或者至少全部显示在屏幕上。

What is currently happening is that when a new notification is sent, it will replace the notification that was there before, instead of simply being added.当前发生的是,当发送新通知时,它将替换之前存在的通知,而不是简单地添加。 I specified a threadIdentifier that I keep the same, as well as a categoryIdentifier, that also is always the same.我指定了一个保持不变的 threadIdentifier,以及一个始终相同的 categoryIdentifier。

Here's the code:这是代码:

            let content = UNMutableNotificationContent()
            content.title = "Title"
            content.body = "Message"
            content.threadIdentifier = "notification"
            content.categoryIdentifier = "notification"
            let request = UNNotificationRequest(identifier: "Stock Changed", content: content, trigger: nil)
            let center = UNUserNotificationCenter.current()
            center.add(request) { (error : Error?) in
                if let theError = error {
                    print(theError.localizedDescription)
                }
            }

How can I make sure notifications don't replace each other?如何确保通知不会相互替换? Thanks!谢谢!

Notifications remove older ones with the same identifier.通知删除具有相同标识符的旧通知。 So if you want to prevent them from replacing each other, you need to provide unique identifiers:因此,如果您想防止它们相互替换,则需要提供唯一标识符:

eg例如

let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: nil)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM