Swift version: 5.2
If your app shows notifications that can be split into sensible groups – such as messages from a person, updates for a news story, scores from a sports match, and so on on – you can have iOS group them together using the
summaryArgument properties of
UNMutableNotificationContent. iOS will then show those messages together, rather than in a long chain mixed up with other messages.
For example, you might write code like this:
let content = UNMutableNotificationContent() content.title = reminder.title content.threadIdentifier = "F39-C521-A7A"
The “F39-C521-A7A” part is a free text string – it won’t be shown to users, but it’s what allows iOS to group things together so it should be unique enough that you don’t get message crossover. If you were building a messaging app you might use the user’s unique identifier for your notification rather than their name, to avoid two messages from different people called Andrew being grouped together.
If you want to customize this further, you can also set the
summaryArgument property of your notification content. This is a string that is shown to users, so you might write something like this:
content.summaryArgument = messageSender.name
That will make iOS write something “5 more alerts from Andrew” in small text underneath the alert, which helps make it clear to users which reminder group that alert belongs to.
SPONSORED Catch bugs as soon as they happen and know exactly why a crash occurred. Instabug's SDK grabs all the logs they need to fix bugs, crashes and performance issues in minutes instead of days. Get screenshots, device details, network logs, repro steps, and tons of other critical insights needed to resolve issues and prioritize product backlogs straight from your dashboard. It only takes a minute to integrate!
Available from iOS 12.0 – learn more in my book
This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.
Link copied to your pasteboard.