With iOS 12 we can set the group of notifications
Thread Identifier
Create notification content with threadIdentifier
to create group of that notification. Group will be of the application or specific topic from an application.
1 2 3 4 5 |
// Creating Groups with Thread Identifiers let content = UNMutableNotificationContent() content.title = "Notifications Group" content.body = "Tutorial by Ashish Kakkad" content.threadIdentifier = "notify-team-ios" |
Notification payload will be like this
1 2 3 4 5 6 7 8 9 |
{ "aps" : { "alert" : { "title" : "Notifications Group", "body" : "Tutorial by Ashish Kakkad" } "thread-id" : "notify-team-ios" } } |
Give meaningful name to thread identifier for specific purpose of group.
Summary of group
Simple Notification Group Summary
1 2 |
let summaryFormat = "%u more messages" return UNNotificationCategory(identifier: "category-identifier", actions: [], intentIdentifiers: [], hiddenPreviewsBodyPlaceholder: nil, categorySummaryFormat: summaryFormat, options: []) |
Hidden Previews Summary Customization
1 2 3 |
let summaryFormat = "%u more messages" let hiddenPreviewsPlaceholder = "%u messages" return UNNotificationCategory(identifier: "category-identifier", actions: [], intentIdentifiers: [], hiddenPreviewsBodyPlaceholder: hiddenPreviewsPlaceholder, categorySummaryFormat: summaryFormat, options: []) |
Notification Group Summary with Arguments
1 2 |
let summaryFormat = "%u more messages from %@" return UNNotificationCategory(identifier: "group-messages", actions: [], intentIdentifiers: [], hiddenPreviewsBodyPlaceholder: nil, categorySummaryFormat: summaryFormat, options: []) |
Notification Group Summary Argument
1 2 3 4 |
let content = UNMutableNotificationContent() content.body = "…" content.threadIdentifier = "notify-team-ios" content.summaryArgument = "Ashish" |
Notification Summary with Argument Count
1 2 3 4 5 |
let content = UNMutableNotificationContent() content.body = "…" content.threadIdentifier = "notify-team-ios" content.summaryArgument = "Ashish" content.summaryArgumentCount = 2 |
Updated notification payload will be like this
1 2 3 4 5 6 7 8 9 10 |
{ "aps" : { "alert" : { "body" : "…", "summary-arg" : "Ashish", "summary-arg-count" : 2 }, "thread-id" : "notify-team-ios" } } |
Example with local notifications:
Setup user notifications in the application.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
// // AppDelegate.swift // AKGroupedNotifications // // Created by Ashish Kakkad on 21/06/18. // Copyright © 2018 Kode. All rights reserved. // import UIKit import UserNotifications @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. let center = UNUserNotificationCenter.current() center.delegate = self let options: UNAuthorizationOptions = [.alert, .sound] center.requestAuthorization(options: options) { (granted, error) in if !granted { print("Something went wrong") } } center.getNotificationSettings { (settings) in if settings.authorizationStatus != .authorized { // Notifications not allowed } } return true } } extension AppDelegate : UNUserNotificationCenterDelegate { func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { completionHandler([.alert, .sound]) } } |
Fire local notifications and look into the thread identifier, summary and summary count…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
if #available(iOS 10.0, *) { for i in 1...5 { let content = UNMutableNotificationContent() content.categoryIdentifier = "AKNotification" content.title = "Notification \(i)" content.body = "\(Date())" if i == 2 { content.threadIdentifier = "ak-notingroup" } else { content.threadIdentifier = "ak-group-website" content.summaryArgumentCount = 1 content.summaryArgument = "by ashishkakkad.com" } let trigger = UNTimeIntervalNotificationTrigger(timeInterval: TimeInterval(10*i), repeats: false) let request = UNNotificationRequest(identifier: "NotificationID\(i)", content: content, trigger: trigger) let center = UNUserNotificationCenter.current() center.add(request) { (error) in print(error?.localizedDescription ?? "") } } } |
Conclusion
I know that I haven’t described anything in detail. If I get time I will update this blog with details.
Happy Coding ?
If you have any questions, comments, suggestions or feedback then contact me on Twitter @ashishkakkad8.