WWDC 2018:iOS 12 通知的新特性

Session 710 : What’s New in User Notificationsswift

iOS 10 新增的 UserNotifications.framework 用一套易用的接口替換了以前版本雜亂的接口,是一次通知接口的大重構。而 iOS 12 則從用戶體驗的角度爲通知帶來了諸如通知分類等便捷的新特性, 通知內容擴展也在這次更新中得到了更強的交互能力。本文也從這兩個方面對 iOS 12 通知特性的變動進行介紹。安全

新增特性

1. 應用通知分組( Grouped Notifications )

iOS 12 中同一類型的通知會被合成一個通知組,用戶能夠經過點擊通知組展開組裏的全部通知。 app

通知分組使用兩種分組方式:自動分組( Automatic grouping ) 和線程標識( Thread identifier )。開發者不須要對自動分組作額外的操做,系統會根據 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 ) 注:若是用戶選擇了 按應用 分組,系統會無視你設定的線程標識只經過 bundle id 對通知進行分組。

2. 通知中心新增通知管理界面

因爲如今的用戶管理單個應用通知設置的入口太深,iOS 12 推出了全新的推送管理頁面以便用戶更快捷的操做。 ide

在新的通知管理頁面中咱們能夠看到兩個明顯的按鈕:隱式推送( Deliver Quietly ) 和 關閉( Turn Off ) 。 隱式推送(根據用戶行爲有時展現爲顯式推送)是 iOS 12 爲方便用戶對通知設置的兩種便捷模式之一:

  • 隱式推送(Deliver Quietly) 隱式推送只會顯示在通知中心,不會帶有聲音提醒,應用角標。
  • 顯式推送(Deliver Prominently) 顯示推送會開啓全部的通知選項。

展現管理頁面有 3 種方式:post

  • 左滑通知,點擊 管理 按鈕
  • 進入通知詳情頁面,點擊右上角的 更多 按鈕
  • 系統根據用戶行爲自動的給出提示,點擊 管理 按鈕

自定義通知設置頁面( Custom Settings )

因爲新的通知管理頁面也支持用戶關閉通知提示,App 能夠爲通知提供更詳細的管理頁面引導用戶關閉部分他們不但願收到的推送而不是關閉全部。當用戶點擊通知設置頁面對應的按鈕時,iOS 12 提供了新的代理方法獲取這個事件並處理。 注:代理中 notification 參數是個 optional ,當用戶從設置頁面點擊管理通知時這個參數會是 nil 。ui

import UIKit
import UserNotifications

class AppDelegate: UIApplicationDelegate, UNUserNotificationCenterDelegate {

    func userNotificationCenter(_ center: UNUserNotificationCenter, openSettingsFor notification: UNNotification? ) {

    }

}
複製代碼

3. 無需用戶受權也能給用戶推送的新機制:臨時受權( Provisional Authorization )

iOS 12 提供了一種新的通知受權機制:臨時受權。這種機制不會給用戶受權的彈窗而直接嘗試給用戶推送,須要注意的是臨時受權推送的消息只會以隱式推送的方式展現給用戶。 spa

在代碼中咱們只須要設置參數 provisional 就能使用這種機制。線程

let notificationCenter = UNUserNotificationCenter.current()

notificationCenter.requestAuthorization(options:[.badge, .sound, .alert, .provisional]) {

}
複製代碼

4. 勿擾模式下依然能夠收到的通知:重要提醒( Critical Alerts )

當咱們在開會或者參與一些很重要的活動時,一般會開啓勿擾模式或者關閉鈴聲,但這個操做有可能會讓咱們錯過一些關鍵的通知。 iOS 12 中加入的重要提醒可以無視勿擾模式和鈴聲開關的限制,收到這類通知時會伴隨一個系統或 App 設定的提示音。須要推送重要提醒的應用須要前往 developer.apple.com/contact/req… 得到受權。3d

注:所謂的重要提醒是指那些須要用戶即刻作出反應的通知,例如與醫療和健康相關的通知,與家庭安全相關的通知,與公共安全相關的通知等。

通知設置頁面重要提醒的開關區別於普通的通知開關

系統容許只接收重要提醒

在代碼中咱們須要設置參數 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
        }
    }
}
複製代碼

舊有特性升級

1. 通知內容擴展升級,更具交互性( Notification Content Extensions )

Notification Content Extensions 是 iOS 10 新增的通知擴展之一,在 iOS 12 中獲得了加強,不熟悉的同窗能夠經過喵神的文章瞭解下: 活久見的重構 - iOS 10 UserNotifications 框架解析

通知動做( notification actions )

iOS 12 提供了新的 API 來解決動做當前動做存在的兩個問題:

  • 沒法動態修改
  • 與 category 聯繫緊密
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
        }
    }
}
複製代碼

通知擴展界面新增可交互狀態( User interaction )

Content view 是默認不可交互的,iOS 12 想要將其設置爲可交互的狀態,只須要在 plist 中 將 UNNotificationExtensionUserInteractionEnabled 設置爲 true 便可。

經過 API 調用啓動 App( Launch application )

某些場景下咱們會在通知內容擴展中加入展現全部評論的功能,而且展現操做只有在應用內才能完成,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 調用隱藏通知內容擴展界面( Dismiss content extension view )

若是咱們想在通知內容擴展上增長一個‘喜歡’按鈕,用戶點擊後自動關閉當前的擴展,新增長的 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 專題目錄 - 簡書

相關文章
相關標籤/搜索