Providing a custom summary message for your notification group

When iOS displays a grouped notification, a summary message is shown at the bottom of the notification bubble. By default, this message simply informs the user about the number of notifications that are available. You can customize this message by providing a summary format yourself.

Summary formats are tied to notifications through categories. This means that you can add different messages for different notification categories. A custom summary format is supplied in the form of a format string. Update the UNNotificationCategoryIdentifier initialization code in AppDelegate.swift to add a custom summary format for the reminder category as follows:

let summaryFormat = "%u more reminders for this topic."

let reminderCategory = UNNotificationCategory(identifier: "reminder", actions: [completeAction], intentIdentifiers: [], hiddenPreviewsBodyPlaceholder: nil, categorySummaryFormat: summaryFormat, options: [])

The summary format passed to the UNNotificationCategory initializer uses the %u format specifier to specify where the number of reminders should be displayed. When you run the app and schedule a couple of notifications, the custom summary format should be displayed, as shown in the following screenshot:

Note that one of the notification groups shows a slightly strange summary message. It says 2 more reminders for this topic. Note the plural for reminders being used in this summary message. It would be much nicer if the summary format could account for this and use "1 more reminder for this topic" instead, if there is only a single extra item in the notification group.

You can achieve this by implementing a localized string for the summary format. To do this, start by defining the summary format string in AppDelegate as follows:

let summaryFormat = NSString.localizedUserNotificationString(forKey: "REMINDER_SUMMARY", arguments: nil)

The preceding code defines the summary format as a localized string that is specific for notifications. When you define a string like this, iOS will look for the display string in a dedicated file containing all of the strings that you wish to localize for notifications, using the key you have specified.

Localizations for notifications use a special .stringsdict localization file that contains information that describes how to localize a string for several quantities. To add a .stringsdict file, go to File | New and select the Stringsdict file from the list of file types. Name your file Localizable.stringsdict and make sure it's added to your app target. Next, open the Localizable.stringsdict file and make sure its contents resemble the following screenshot:

The strings dictionary contains the key that was used in AppDelegate to identify the string that should be used for the summary label. The NSStringLocalizedFormatKey string contains a variable that is represented as %#@reminders@. This variable is the part of the string that you want to localize. In this case, the variable represents the entire message.

The variable name is then added as a key to the REMINDER_SUMMARY dictionary to specify the localization rules. In this case, a rule is added for a single item and for multiple items.

If you want to know more about the stringsdict file, make sure to read Apple's documentation on app localization.

Now that you know how to implement grouped notifications, let's see how you can add Notification Extensions to your app.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.51.117