WWDC 2018:iOS 12 通知的新特性
Session 710 : What’s New in User Notifications
iOS 10 新增的 UserNotifications.framework 用一套易用的接口替換了之前版本雜亂的接口,是一次通知接口的大重構。而 iOS 12 則從用戶體驗的角度為通知帶來了諸如通知分類等便捷的新特性, 通知內(nèi)容擴展也在此次更新中獲得了更強的交互能力。本文也從這兩個方面對 iOS 12 通知特性的變更進行介紹。
新增特性
1. 應用通知分組( Grouped Notifications )
iOS 12 中同一類型的通知會被合成一個通知組,用戶可以通過點擊通知組展開組里的所有通知。
通知分組使用兩種分組方式:自動分組( Automatic grouping ) 和線程標識( Thread identifier )。開發(fā)者不需要對自動分組做額外的操作,系統(tǒng)會根據(jù) App 的 bundle id 對推送進行分組。如果需要對通知做更細致的分組就需要用上線程標識了。 // 本地通知 let content = UNMutableNotificationContent() content.title = "New Photo" content.body = "Jane Doe posted a new photo" // 自定義標識 content.threadIdentifier = "thread-identifier"// 遠程通知 {"aps" : {"alert" : {"title" : "New Photo","body" : "Jane Doe posted a new photo","thread-id" : "thread-identifier",}} } 復制代碼用戶可以在通知管理頁面對通知分組進行管理:
- 自動( Automatic )
- 按應用( By App )
- 關閉( Off ) 注:如果用戶選擇了 按應用 分組,系統(tǒng)會無視你設定的線程標識只通過 bundle id 對通知進行分組。
2. 通知中心新增通知管理界面
由于現(xiàn)在的用戶管理單個應用通知設置的入口太深,iOS 12 推出了全新的推送管理頁面以便用戶更快捷的操作。
在新的通知管理頁面中我們可以看到兩個明顯的按鈕:隱式推送( Deliver Quietly ) 和 關閉( Turn Off ) 。 隱式推送(根據(jù)用戶行為有時展示為顯式推送)是 iOS 12 為方便用戶對通知設置的兩種便捷模式之一:- 隱式推送(Deliver Quietly) 隱式推送只會顯示在通知中心,不會帶有聲音提醒,應用角標。
- 顯式推送(Deliver Prominently) 顯示推送會開啟所有的通知選項。
展示管理頁面有 3 種方式:
- 左滑通知,點擊 管理 按鈕
- 進入通知詳情頁面,點擊右上角的 更多 按鈕
- 系統(tǒng)根據(jù)用戶行為自動的給出提示,點擊 管理 按鈕
自定義通知設置頁面( Custom Settings )
由于新的通知管理頁面也支持用戶關閉通知提示,App 可以為通知提供更詳細的管理頁面引導用戶關閉部分他們不希望收到的推送而不是關閉所有。當用戶點擊通知設置頁面對應的按鈕時,iOS 12 提供了新的代理方法獲取這個事件并處理。 注:代理中 notification 參數(shù)是個 optional ,當用戶從設置頁面點擊管理通知時這個參數(shù)會是 nil 。
import UIKit import UserNotificationsclass AppDelegate: UIApplicationDelegate, UNUserNotificationCenterDelegate {func userNotificationCenter(_ center: UNUserNotificationCenter, openSettingsFor notification: UNNotification? ) {}} 復制代碼3. 無需用戶授權也能給用戶推送的新機制:臨時授權( Provisional Authorization )
iOS 12 提供了一種新的通知授權機制:臨時授權。這種機制不會給用戶授權的彈窗而直接嘗試給用戶推送,需要注意的是臨時授權推送的消息只會以隱式推送的方式展示給用戶。
在代碼中我們只需要設置參數(shù) provisional 就能使用這種機制。
let notificationCenter = UNUserNotificationCenter.current()notificationCenter.requestAuthorization(options:[.badge, .sound, .alert, .provisional]) {} 復制代碼4. 勿擾模式下依然可以收到的通知:重要提醒( Critical Alerts )
當我們在開會或者參與一些很重要的活動時,通常會開啟勿擾模式或者關閉鈴聲,但這個操作有可能會讓我們錯過一些關鍵的通知。 iOS 12 中加入的重要提醒能夠無視勿擾模式和鈴聲開關的限制,收到這類通知時會伴隨一個系統(tǒng)或 App 設定的提示音。需要推送重要提醒的應用需要前往 developer.apple.com/contact/req… 獲得授權。
注:所謂的重要提醒是指那些需要用戶即刻做出反應的通知,例如與醫(yī)療和健康相關的通知,與家庭安全相關的通知,與公共安全相關的通知等。
通知設置頁面重要提醒的開關區(qū)別于普通的通知開關
系統(tǒng)允許只接收重要提醒在代碼中我們需要設置參數(shù) criticalAlert ,用戶會看到單獨的重要提醒授權
let notificationCenter = UNUserNotificationCenter.current()notificationCenter.requestAuthorization(options:[.badge, .sound, .alert, .criticalAlert]) {} 復制代碼推送重要提醒通知和普通通知的區(qū)別在于提示音
// 本地重要提醒 let content = UNMutableNotificationContent() content.title = "WARNING: LOW BLOOD SUGAR" content.body = "Glucose level at 57." content.categoryIdentifier = "low-glucose—alert" // 使用系統(tǒng)默認的重要提醒音 content.sound = UNNotificationSound.defaultCritical // 使用自定義的重要提醒音 content.sound = UNNotificationSound.criticalSoundNamed(@"warning-sound" withAudioVolume: 1.00)// 遠程重要提醒 {"aps" : {"sound" : {"critical": 1,"name": "warning-sound.aiff","volume": 1.0}} } 復制代碼舊有特性升級
1. 通知內(nèi)容擴展升級,更具交互性( Notification Content Extensions )
Notification Content Extensions 是 iOS 10 新增的通知擴展之一,在 iOS 12 中得到了增強,不熟悉的同學可以通過喵神的文章了解下: 活久見的重構 - iOS 10 UserNotifications 框架解析。
通知動作( notification actions )
iOS 12 提供了新的 API 來解決動作當前動作存在的兩個問題:
- 無法動態(tài)修改
- 與 category 聯(lián)系緊密
notificationActions 屬性允許你獲取當前擴展的動作,更新新的動作。
class NotificationViewController: UIViewController, UNNotificationContentExtension {func didReceive(_ response: UNNotificationResponse, completionHandler completion: (UNNotificationContentExtensionResponseOption) -> Void) {if response.actionIdentifier == "like-action" {// Update state...let unlikeAction = UNNotificationAction(identifier: "unlike-action", title: "Unlike", options: [])let currentActions = extensionContext?.notificationActionslet commentAction = currentActions![1]let newActions = [ unlikeAction, commentAction ]extensionContext?.notificationActions = newActions}} } 復制代碼通知擴展界面新增可交互狀態(tài)( User interaction )
Content view 是默認不可交互的,iOS 12 想要將其設置為可交互的狀態(tài),只需要在 plist 中 將 UNNotificationExtensionUserInteractionEnabled 設置為 true 即可。通過 API 調(diào)用啟動 App( Launch application )
某些場景下我們會在通知內(nèi)容擴展中加入展示所有評論的功能,并且展示操作只有在應用內(nèi)才能完成,iOS 12 提供了一個新的 API 讓我們能通過代碼啟動 App 。
extension NSExtensionContext {(iOS 12.0, *)func performNotificationDefaultAction()}// Demo: 在 Notification Content Extension 中啟動 App import UserNotificationsUIclass NotificationViewController: UIViewController, UNNotificationContentExtension { var allCommentsButton: UIButton?...allCommentsButton?.addTarget(self, action: #selector(launchApp), for: .touchUpInside)... func launchApp() {extensionContext?.performNotificationDefaultAction()}} 復制代碼當這個 API 被調(diào)用時, App 會被啟動,并調(diào)用代理方法:
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void) {// Handle action response } 復制代碼通過 API 調(diào)用隱藏通知內(nèi)容擴展界面( Dismiss content extension view )
如果我們想在通知內(nèi)容擴展上增加一個‘喜歡’按鈕,用戶點擊后自動關閉當前的擴展,新增加的 API 能夠幫助你實現(xiàn)這一功能。
extension NSExtensionContext {(iOS 12.0, *)func dismissNotificationContentExtension()}// Demo: 在通知內(nèi)容擴展中隱藏通知內(nèi)容擴展頁面 import UserNotificationsUIclass NotificationViewController: UIViewController, UNNotificationContentExtension { var likeButton: UIButton?...likeButton?.addTarget(self, action: #selector(likeButtonTapped), for: .touchUpInside)... func likeButtonTapped() {likedPhoto()extensionContext?.dismissNotificationContentExtension()}} 復制代碼需要注意的是,調(diào)用這個 API 并不會移除那條通知,如果需要移除通知可以調(diào)用下面的 API
class UNUserNotificationCenter {func removeDeliveredNotifications(withIdentifiers identifiers: [String])} 復制代碼總結
有 iOS 10 整體通知框架的打底,iOS 12 在通知的用戶體驗上的提升還是挺顯著的,而通知關閉的成本降低勢必需要內(nèi)容生產(chǎn)方對推送內(nèi)容更加謹慎,或者為部分推送內(nèi)容提供關閉的選項。期待在 iOS 12 上看到利用新特性的有趣應用。
查看更多 WWDC 18 相關文章請前往 老司機x知識小集xSwiftGG WWDC 18 專題目錄 - 簡書
總結
以上是生活随笔為你收集整理的WWDC 2018:iOS 12 通知的新特性的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Shell笔记9——Shell数组的应用
- 下一篇: Javascript数组常见的方法