Session 711 : Using Grouped Notificationshtml
iOS 12 爲通知帶來了很多新特性,而在這些新特性中通知分組最爲吸引眼球。對於此次通知的新特性還不瞭解的同窗能夠點擊這裏。 本文針對這一新特性從如下幾個方面進行闡述:swift
默認狀況下系統會按照應用( bundle id )把收到的通知分紅不一樣的組,每一個組的頂層是該組收到的最新通知,伴隨着當前組的通知摘要( group summaries )。 session
按應用分組的模式沒法知足豐富的顯示通知場景,重要的通知可能會被不重要的通知遮蓋而沒法達到提示的做用,這時候就該自定義分組大顯身手了。 要實現自定義分組開發者只須要給想要額外分組的通知加上線程標識( thread identifier )便可:本地通知直接爲 content 的 threadIdentifier 參數賦值,而對於遠程通知而言,在通知的 payload 中添加以 thread-id 爲鍵的鍵值對。app
// 本地通知
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 中寫法有出入,看官方文檔的解釋應該以這裏爲準。ide
當通知被分組以後,若是沒有一個提示信息告訴咱們當前分組中有多少條信息,這些信息的簡單描述是一件很不方便的事情,蘋果提供了通知摘要爲咱們實現這個功能。post
在通知概要以及如何自定義通知摘要以前,有一個概念須要先拿出來講一下:ui
摘要參數數量表明瞭 1 條通知所包含的內容量。這樣說可能有點抽象,咱們以 Podcase 爲例,Podcast 爲了減小推送的總量,會把某個節目同一時間更新的幾條內容合成一個推送,這就造成了咱們下面看到的狀況。 spa
// 本地推送設置
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"
}
}
複製代碼
瞭解了摘要參數數量以後,咱們來看一下最基本的通知摘要組成格式。線程
let summaryFormat = "%u more messages"
return UNNotificationCategory(
identifier: "category-identifier",
actions: [],
intentIdentifiers: [],
hiddenPreviewsBodyPlaceholder: nil,
categorySummaryFormat: summaryFormat,
options: []
)
複製代碼
summaryFormat 中使用了 %u 來爲通知組中全部未展現通知的摘要參數數量之和佔位( 未展現通知指通知組中除了棧頂通知的其他通知 )。設計
咱們能夠看到在 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 專題目錄 - 簡書