简体   繁体   中英

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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