iOS 12: notification grouping

    This article, like the previous one , is devoted to the changes in Notifications that appeared in iOS 12.

    In the past, I talked about the general innovations, this time I’ll dwell more on the grouping of notifications.

    Grouping in iOS 12

    In order for notifications coming from your application to be grouped in iOS 12, you do not need to do anything, the grouping will be performed automatically. Notifications on the screen will begin to stack up when their number reaches 5.

    The user has the ability to influence the grouping through the system settings.

    • Automatic (default) - the application controls the grouping settings.
    • By App - application settings are ignored; all notifications from the application are added to a single stack.
    • Off - application settings are ignored, notification grouping is disabled.

    Grouping setup

    We can influence the grouping of messages only if the Automatic flag is used in the settings. To do this, you need to specify the thread-id parameter in the notification . Also pass the thread-id to the subtitle notification for clarity:

    let content = UNMutableNotificationContent()
    content.threadIdentifier = threadId
    content.subtitle = "Thread ID: \(threadId)"

    We send several messages and see the result of grouping:

    You can also pass thread-id to push notifications:

        "aps" : {
            "alert" : {
                "title" : "Cat Title""body" : "Cat Body"
            "thread-id" : "all-cats"

    When you specify an identifier for a notification, do not forget to specify it in UNNotificationCategory , otherwise in the Extension for notifications they will not be processed:

    UNNotificationCategory(identifier: "Cat-Category",
                           actions: actions,
                           intentIdentifiers: ["all-cats", "3-star-cats"],
                           options: [])

    For a category, you can specify the message format that will be displayed to the user on the device; this is another of the innovations of iOS 12. This can be done only when the category is initialized:

    UNNotificationCategory(identifier: "category-simple",
                           actions: actions,
                           intentIdentifiers: ["all-cats", "3-star-cats"],
                           hiddenPreviewsBodyPlaceholder: nil,
                           categorySummaryFormat: "%u новых котиков в разделе %@",
                           options: [])

    The % u parameter is responsible for the number of messages in the group, it can be modified with the help of the summaryArgumentCount argument . For example, you published a new collection of cats, in which 10 new pictures. It makes no sense to send the user 10 guns, you can send one message with a counter, equal to 10. The parameter % @ will display the value of the variable summaryArgument :

    let content = UNMutableNotificationContent()
    content.summaryArgumentCount = 10
    content.summaryArgument = "Все Коты"

    In each group we have 3 messages, but due to the fact that the parameter summaryArgumentCount is specified , the number of messages is displayed as 20. And summaryArgument allows us to add the name of the category to which the push notifications refer. Not bad, but now we have a problem with localization.


    To implement the task of localization, add to the project Stringdict File .

    We change the Localized String Key key to NOTIFICATION_SUMMARY , otherwise the values ​​from the dictionary will not be processed, despite the fact that when a localized string is received, the key with the name of the dictionary is passed as a parameter. The remaining values ​​are shown in the figure:

    In the File Inspector for the dictionary, select Localize .

    Specify files for localization.

    Go to the project settings and add a language to localize Russian (ru) .

    Extra files can be removed from the project.

    In the code, we get a localized string and pass it as the categorySummaryFormat parameter :

    let summary = NSString.localizedUserNotificationString(forKey: "NOTIFICATION_SUMMARY",
                                                                         arguments: nil)
    let category = UNNotificationCategory(identifier: "Cat-Category",
                                          actions: actions,
                                          intentIdentifiers: ["all-cats", "3-star-cats"],
                                          hiddenPreviewsBodyPlaceholder: nil,
                                          categorySummaryFormat: summary,
                                          options: [])

    As a result, we get excellent localization of the application.

    The project created for the previous article has been updated, the code can be found on github . Two reports at WWDC were devoted to the implementation of notifications: What's New in User Notifications and Using Grouped Notifications . We can discuss innovations at MBLT DEV 2018 in Moscow on September 28.

    Have a good week everyone! ^ _ ^

    Also popular now: