Session 711 : Using Grouped Notificationshtml
iOS 12 为通知带来了很多新特性,而在这些新特性中通知分组最为吸引眼球。对于此次通知的新特性还不了解的同窗能够点击这里。 本文针对这一新特性从如下几个方面进行阐述:swift
默认状况下系统会按照应用( bundle id )把收到的通知分红不一样的组,每一个组的顶层是该组收到的最新通知,伴随着当前组的通知摘要( group summaries )。 markdown
按应用分组的模式没法知足丰富的显示通知场景,重要的通知可能会被不重要的通知遮盖而没法达到提示的做用,这时候就该自定义分组大显身手了。 要实现自定义分组开发者只须要给想要额外分组的通知加上线程标识( thread identifier )便可:本地通知直接为 content 的 threadIdentifier 参数赋值,而对于远程通知而言,在通知的 payload 中添加以 thread-id 为键的键值对。session
// 本地通知 let content = UNMutableNotificationContent() content.title = "Notifications Team" content.body = "WWDC session after party" // 自定义标识 content.threadIdentifier = "notifications-team-chat" // 远程通知 { "aps" : { "alert" : { "title" : "New Photo", "body" : "WWDC session after party" } // 自定义标识 "thread-id" : "notifications-team-chat" } } 复制代码
注:Session 711 和 Session 710 在远程推送的 payload 中写法有出入,看官方文档的解释应该以这里为准。app
当通知被分组以后,若是没有一个提示信息告诉咱们当前分组中有多少条信息,这些信息的简单描述是一件很不方便的事情,苹果提供了通知摘要为咱们实现这个功能。ide
在通知概要以及如何自定义通知摘要以前,有一个概念须要先拿出来讲一下:oop
摘要参数数量表明了 1 条通知所包含的内容量。这样说可能有点抽象,咱们以 Podcase 为例,Podcast 为了减小推送的总量,会把某个节目同一时间更新的几条内容合成一个推送,这就造成了咱们下面看到的状况。 post
// 本地推送设置 let content = UNMutableNotificationContent() content.body = "…" content.threadIdentifier = "…" content.summaryArgument = "Song by Song" content.summaryArgumentCount = 3 // 远程推送设置 { "aps" : { "alert" : { "body" : "…", "summary-arg" : "Song by Song", "summary-arg-count" : 3 }, "thread-id" : "notifications-team" } } 复制代码
了解了摘要参数数量以后,咱们来看一下最基本的通知摘要组成格式。ui
let summaryFormat = "%u more messages" return UNNotificationCategory( identifier: "category-identifier", actions: [], intentIdentifiers: [], hiddenPreviewsBodyPlaceholder: nil, categorySummaryFormat: summaryFormat, options: [] ) 复制代码
summaryFormat 中使用了 %u 来为通知组中全部未展现通知的摘要参数数量之和占位( 未展现通知指通知组中除了栈顶通知的其他通知 )。spa
咱们能够看到在 UNNotificationCategory 初始化时能够设置的参数中有另一个参数 hiddenPreviewsBodyPlaceholder ,若是用户设置了在锁屏状况下不展现通知具体内容( iOS 11 新增的特性 ),咱们能够设定这个参数来展现不一样的摘要内容。
let summaryFormat = "%u more messages" let hiddenPreviewsPlaceholder = "%u messages" return UNNotificationCategory( identifier: "category-identifier", actions: [], intentIdentifiers: [], hiddenPreviewsBodyPlaceholder: hiddenPreviewsPlaceholder, categorySummaryFormat: summaryFormat, options: [] ) 复制代码
在 hiddenPreviewsBodyPlaceholder 也是用了 %u 来为通知组中全部通知的摘要参数数量之和占位。
为了更好的理解这两种状况的区别,咱们依然以 Podcast 的那张图为例。若是咱们设置了不展现通知具体内容,通知组的摘要中会显示:“ 9 new episodes are available. ” ( 2 + 3 + 1 + 3 );而若是咱们没有设置,则会显示:“ 7 new episodes are available. ” ( 3 + 1 + 3 )。
let summaryFormat = "%u more messages from %@" return UNNotificationCategory( identifier: "group-messages", actions: [], intentIdentifiers: [], hiddenPreviewsBodyPlaceholder: nil, categorySummaryFormat: summaryFormat, options: [] ) 复制代码
咱们使用 %@ 来为摘要参数占位。那什么是摘要参数?
和摘要参数数量同样,摘要参数是 iOS 12 UNNotificationContent 新增长的用来拼成 UNNotificationCategory 的摘要格式化字符串。
// 本地推送设置 let content = UNMutableNotificationContent() content.body = "…" content.threadIdentifier = "notifications-team" content.summaryArgument = "Kritarth" // 远程推送设置 { "aps" : { "alert" : { "body" : "…", "summary-arg" : "Kritarth" }, "thread-id" : "notifications-team" } } 复制代码
iOS 系统会为咱们将未展现的通知中的摘要参数合成一个字符串展现给用户。 注:摘要参数不须要每条通知都不一样,咱们可使用相同的摘要参数,系统会筛选不一样的摘要参数进行字符串合成,下图中邮件就是用 iCloud 邮件帐户做为摘要参数。
若是咱们须要让 App 应对不一样语言的场景,只须要两步,简直比把大象放进冰箱还方便!
let summaryFormat = NSString.localizedUserNotificationString( forKey: "NOTIFICATION_SUMMARY", arguments: nil ) 复制代码
因为通知摘要是定义在 UNNotificationCategory ,而线程标识是定义在通知内容中,有时候会碰到须要合并同个通知组中不一样的摘要的状况。
public protocol UNNotificationContentExtension : NSObjectProtocol { public func didReceive(_ notification: UNNotification) } 复制代码
class UNUserNotificationCenter : NSObject { func getDeliveredNotifications(completionHandler:([UNNotification]) -> Swift.Void) } 复制代码
public protocol UNNotificationContentExtension : NSObjectProtocol { public func didReceive(_ notification: UNNotification) } 复制代码
class UNUserNotificationCenter : NSObject { func removeDeliveredNotifications(withIdentifiers identifiers: [String]) } 复制代码
Session 中以系统的 3 个 App 为例提出了在设计通知分类的时候须要注意的地方。
通知分组强化了 App 推送的信息展现能力,对于开发者来讲为通知设置线程标识也十分便捷。相信随着 iOS 12 的到来能有愈来愈多的 App 利用自定义通知分组的特性为用户提供更有效的通知。
查看更多 WWDC 18 相关文章请前往 老司机x知识小集xSwiftGG WWDC 18 专题目录 - 简书