Session 710 : What’s New in User Notificationsswift
iOS 10 新增的 UserNotifications.framework 用一套易用的接口替換了以前版本雜亂的接口,是一次通知接口的大重構。而 iOS 12 則從用戶體驗的角度爲通知帶來了諸如通知分類等便捷的新特性, 通知內容擴展也在這次更新中得到了更強的交互能力。本文也從這兩個方面對 iOS 12 通知特性的變動進行介紹。安全
iOS 12 中同一類型的通知會被合成一個通知組,用戶能夠經過點擊通知組展開組裏的全部通知。 markdown
// 本地通知 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", } } } 複製代碼
用戶能夠在通知管理頁面對通知分組進行管理:app
因爲如今的用戶管理單個應用通知設置的入口太深,iOS 12 推出了全新的推送管理頁面以便用戶更快捷的操做。 框架
展現管理頁面有 3 種方式:ide
因爲新的通知管理頁面也支持用戶關閉通知提示,App 能夠爲通知提供更詳細的管理頁面引導用戶關閉部分他們不但願收到的推送而不是關閉全部。當用戶點擊通知設置頁面對應的按鈕時,iOS 12 提供了新的代理方法獲取這個事件並處理。 注:代理中 notification 參數是個 optional ,當用戶從設置頁面點擊管理通知時這個參數會是 nil 。oop
import UIKit import UserNotifications class AppDelegate: UIApplicationDelegate, UNUserNotificationCenterDelegate { func userNotificationCenter(_ center: UNUserNotificationCenter, openSettingsFor notification: UNNotification? ) { } } 複製代碼
iOS 12 提供了一種新的通知受權機制:臨時受權。這種機制不會給用戶受權的彈窗而直接嘗試給用戶推送,須要注意的是臨時受權推送的消息只會以隱式推送的方式展現給用戶。 post
在代碼中咱們只須要設置參數 provisional 就能使用這種機制。ui
let notificationCenter = UNUserNotificationCenter.current() notificationCenter.requestAuthorization(options:[.badge, .sound, .alert, .provisional]) { } 複製代碼
當咱們在開會或者參與一些很重要的活動時,一般會開啓勿擾模式或者關閉鈴聲,但這個操做有可能會讓咱們錯過一些關鍵的通知。 iOS 12 中加入的重要提醒可以無視勿擾模式和鈴聲開關的限制,收到這類通知時會伴隨一個系統或 App 設定的提示音。須要推送重要提醒的應用須要前往 developer.apple.com/contact/req… 得到受權。spa
注:所謂的重要提醒是指那些須要用戶即刻作出反應的通知,例如與醫療和健康相關的通知,與家庭安全相關的通知,與公共安全相關的通知等。
通知設置頁面重要提醒的開關區別於普通的通知開關
在代碼中咱們須要設置參數 criticalAlert ,用戶會看到單獨的重要提醒受權
let notificationCenter = UNUserNotificationCenter.current() notificationCenter.requestAuthorization(options:[.badge, .sound, .alert, .criticalAlert]) { } 複製代碼
推送重要提醒通知和普統統知的區別在於提示音
// 本地重要提醒 let content = UNMutableNotificationContent() content.title = "WARNING: LOW BLOOD SUGAR" content.body = "Glucose level at 57." content.categoryIdentifier = "low-glucose—alert" // 使用系統默認的重要提醒音 content.sound = UNNotificationSound.defaultCritical // 使用自定義的重要提醒音 content.sound = UNNotificationSound.criticalSoundNamed(@"warning-sound" withAudioVolume: 1.00) // 遠程重要提醒 { "aps" : { "sound" : { "critical": 1, "name": "warning-sound.aiff", "volume": 1.0 } } } 複製代碼
Notification Content Extensions 是 iOS 10 新增的通知擴展之一,在 iOS 12 中獲得了加強,不熟悉的同窗能夠經過喵神的文章瞭解下: 活久見的重構 - iOS 10 UserNotifications 框架解析。
iOS 12 提供了新的 API 來解決動做當前動做存在的兩個問題:
extension NSExtensionContext { @available(iOS 12.0, *) var notificationActions: [UNNotificationAction] } 複製代碼
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?.notificationActions let commentAction = currentActions![1] let newActions = [ unlikeAction, commentAction ] extensionContext?.notificationActions = newActions } } } 複製代碼
某些場景下咱們會在通知內容擴展中加入展現全部評論的功能,而且展現操做只有在應用內才能完成,iOS 12 提供了一個新的 API 讓咱們能經過代碼啓動 App 。
extension NSExtensionContext { @available(iOS 12.0, *) func performNotificationDefaultAction() } // Demo: 在 Notification Content Extension 中啓動 App import UserNotificationsUI class NotificationViewController: UIViewController, UNNotificationContentExtension { @IBOutlet var allCommentsButton: UIButton? ... allCommentsButton?.addTarget(self, action: #selector(launchApp), for: .touchUpInside) ... @objc func launchApp() { extensionContext?.performNotificationDefaultAction() } } 複製代碼
當這個 API 被調用時, App 會被啓動,並調用代理方法:
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void) { // Handle action response } 複製代碼
若是咱們想在通知內容擴展上增長一個‘喜歡’按鈕,用戶點擊後自動關閉當前的擴展,新增長的 API 可以幫助你實現這一功能。
extension NSExtensionContext { @available(iOS 12.0, *) func dismissNotificationContentExtension() } // Demo: 在通知內容擴展中隱藏通知內容擴展頁面 import UserNotificationsUI class NotificationViewController: UIViewController, UNNotificationContentExtension { @IBOutlet var likeButton: UIButton? ... likeButton?.addTarget(self, action: #selector(likeButtonTapped), for: .touchUpInside) ... @objc func likeButtonTapped() { likedPhoto() extensionContext?.dismissNotificationContentExtension() } } 複製代碼
須要注意的是,調用這個 API 並不會移除那條通知,若是須要移除通知能夠調用下面的 API
class UNUserNotificationCenter { func removeDeliveredNotifications(withIdentifiers identifiers: [String]) } 複製代碼
有 iOS 10 總體通知框架的打底,iOS 12 在通知的用戶體驗上的提高仍是挺顯著的,而通知關閉的成本下降勢必須要內容生產方對推送內容更加謹慎,或者爲部分推送內容提供關閉的選項。期待在 iOS 12 上看到利用新特性的有趣應用。
查看更多 WWDC 18 相關文章請前往 老司機x知識小集xSwiftGG WWDC 18 專題目錄 - 簡書