[iOS] 通知詳解:iOS 10 UserNotifications API

通知相關(guān)系列文章
iOS10 之前通知使用介紹
[iOS] 通知詳解: UIUserNotification
iOS10 相關(guān)API
[iOS] 通知詳解:iOS 10 UserNotifications API
iOS10 本地/遠(yuǎn)程通知
[iOS] 通知詳解: iOS 10 UserNotifications
iOS10 通知附加包
[iOS] 通知詳解: iOS 10 UserNotifications -- 附加包Media Attachments
iOS10 自定義UI
[iOS] 通知詳解: iOS 10 UserNotifications -- 自定義通知UI

iOS 10中引入 UserNotifications ,用來取代之前的通知處理方式,并增加了很多新的特性,來豐富通知的功能,使用時需要引入 UserNotifications 頭文件,并遵循協(xié)議 UNUserNotificationCenterDelegate

import UserNotifications

由于UserNotifications的內(nèi)容較多,開始寫在一篇文章進(jìn)行介紹,導(dǎo)致文章篇幅過長,所以進(jìn)行了簡單拆分,本文主要是介紹常用的一些API,如果想直接看使用方法,可以跳過,不清楚的再回來查看有無相關(guān)的介紹。

相關(guān)類庫介紹

UserNotifications 模塊主要涉及到以下一些類庫

用戶通知中心 UNUserNotificationCenter

主要管理通知相關(guān)的調(diào)度,添加,其相關(guān)的屬性和方法如下

// The delegate can only be set from an application 代理
    weak open var delegate: UNUserNotificationCenterDelegate?
 // 當(dāng)前設(shè)備是否支持的擴(kuò)展
    // Returns YES if the current device supports content extensions
    open var supportsContentExtensions: Bool { get }

    // 獲取當(dāng)前的通知中心(單例)
    // The UNUserNotificationCenter for the current application
    open class func current() -> UNUserNotificationCenter

    // 請求通知的權(quán)限,參數(shù)為 UNAuthorizationOptions 的集合
    // User authorization is required for applications to notify the user using UNUserNotificationCenter via both local and remote notifications.
    open func requestAuthorization(options: UNAuthorizationOptions = [], completionHandler: @escaping (Bool, Error?) -> Void)

    // 通知快捷操作action集合,參數(shù)為 UNNotificationCategory的集合
    // Notification categories can be used to choose which actions will be displayed on which notifications.
    open func setNotificationCategories(_ categories: Set<UNNotificationCategory>)
// 獲取當(dāng)前添加的 Categories
    open func getNotificationCategories(completionHandler: @escaping (Set<UNNotificationCategory>) -> Void)

    // 獲取通知當(dāng)前的設(shè)置,詳見 UNNotificationSettings
    // The application's user notification settings
    open func getNotificationSettings(completionHandler: @escaping (UNNotificationSettings) -> Void)

    // 添加通知的請求
    // Notification requests can be scheduled to notify the user via time and location. See UNNotificationTrigger for more information. Calling -addNotificationRequest: will replace an existing notification request with the same identifier. A notification request with the identifier as an existing delivered notifications will alert for the new notification request and replace the existing delivered notification when it is triggered. The number of pending notification requests that may be scheduled by an application at any one time is limited by the system.
    open func add(_ request: UNNotificationRequest, withCompletionHandler completionHandler: ((Error?) -> Void)? = nil)

    // 獲取所有等待執(zhí)行的通知請求
    // Notification requests that are waiting for their trigger to fire
    open func getPendingNotificationRequests(completionHandler: @escaping ([UNNotificationRequest]) -> Void)
// 取消未執(zhí)行的通知請求
    open func removePendingNotificationRequests(withIdentifiers identifiers: [String])
// 取消所有未執(zhí)行的通知請求
    open func removeAllPendingNotificationRequests()

    // 獲取已添加到通知中心的通知
    // Notifications that have been delivered and remain in Notification Center. Notifications triggered by location cannot be retrieved, but can be removed.
    open func getDeliveredNotifications(completionHandler: @escaping ([UNNotification]) -> Void)
// 移除通知
    open func removeDeliveredNotifications(withIdentifiers identifiers: [String])

    open func removeAllDeliveredNotifications()

通知中心協(xié)議 UNUserNotificationCenterDelegate

其中主要有三個協(xié)議方法

// The method will be called on the delegate only if the application is in the foreground. If the method is not implemented or the handler is not called in a timely manner then the notification will not be presented. The application can choose to have the notification presented as a sound, badge, alert and/or in the notification list. This decision should be based on whether the information in the notification is otherwise visible to the user.
    @available(iOS 10.0, *)
    optional public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)

    
    // The method will be called on the delegate when the user responded to the notification by opening the application, dismissing the notification or choosing a UNNotificationAction. The delegate must be set before the application returns from application:didFinishLaunchingWithOptions:.
    @available(iOS 10.0, *)
    optional public func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)

    
    // The method will be called on the delegate when the application is launched in response to the user's request to view in-app notification settings. Add UNAuthorizationOptionProvidesAppNotificationSettings as an option in requestAuthorizationWithOptions:completionHandler: to add a button to inline notification settings view and the notification settings view in Settings. The notification will be nil when opened from Settings.
    @available(iOS 12.0, *)
    optional public func userNotificationCenter(_ center: UNUserNotificationCenter, openSettingsFor notification: UNNotification?)

  • 第一個協(xié)議方法,是當(dāng)應(yīng)用在前臺運(yùn)行時調(diào)用,其 completionHandler 回調(diào)用于告訴系統(tǒng)以何種方式告知用戶,來了新通知,參數(shù)為UNNotificationPresentationOptions 類型,有三個值可供選擇:badge、alert、sound,如果沒有調(diào)用completionHandler回調(diào),則不會有提醒;在iOS10之前,如果應(yīng)用在前臺運(yùn)行,來了新通知,是無法使用系統(tǒng)提醒的。
  • 第二個協(xié)議方法,是當(dāng)應(yīng)用在后臺或者被殺死,當(dāng)用戶點(diǎn)擊通知內(nèi)容或者通知action時,會調(diào)用該方法,通過 UNNotificationResponse 實(shí)例可獲取詳細(xì)的信息
  • 第三個協(xié)議方法,是在app內(nèi)展示通知的設(shè)置情況,需要在請求權(quán)限的options中添加 providesAppNotificationSettings,iOS12才支持

UNNotificationRequest

主要是針對本地通知,發(fā)起一個通知請求,其主要有一個初始化方法,和三個只讀屬性來獲取相應(yīng)的值;
如果是遠(yuǎn)程通知,代理方法里會有回調(diào)的UNNotificationRequest實(shí)例,直接獲取相應(yīng)的值即可:

// identifier: 唯一標(biāo)識符
// content:要展示的消息內(nèi)容,詳見UNNotificationContent
// trigger:觸發(fā)的方式,詳見 UNNotificationTrigger
public convenience init(identifier: String, content: UNNotificationContent, trigger: UNNotificationTrigger?)

open var identifier: String { get }

    // The content that will be shown on the notification.
    @NSCopying open var content: UNNotificationContent { get }

    // The trigger that will or did cause the notification to be delivered. No trigger means deliver now.
    @NSCopying open var trigger: UNNotificationTrigger? { get }

UNNotificationContent & UNMutableNotificationContent

通知內(nèi)容的承載體,遠(yuǎn)程通知回調(diào)的主要是 UNNotificationContent,創(chuàng)建本地通知內(nèi)容的時候使用 UNMutableNotificationContent

// 通知的附件,iOS10之后,允許通知攜帶一張圖片,一段視頻,一段音頻
// Optional array of attachments.
    open var attachments: [UNNotificationAttachment]

    // 角標(biāo)數(shù)值
    // The application badge number. nil means no change. 0 to hide.
    @NSCopying open var badge: NSNumber?

  // 標(biāo)題,限制一行,多余的以...
// The title of the notification. Use -[NSString localizedUserNotificationStringForKey:arguments:] to provide a string that will be localized at the time that the notification is presented.
    open var title: String

    // 副標(biāo)題,限制一行,多余的以...
    // The subtitle of the notification. Use -[NSString localizedUserNotificationStringForKey:arguments:] to provide a string that will be localized at the time that the notification is presented.
    open var subtitle: String

    // 消息內(nèi)容
    // The body of the notification. Use -[NSString localizedUserNotificationStringForKey:arguments:] to provide a string that will be localized at the time that the notification is presented.
    open var body: String

    // 所屬的 category 標(biāo)識符
    // The identifier for a registered UNNotificationCategory that will be used to determine the appropriate actions to display for the notification.
    open var categoryIdentifier: String

    // 點(diǎn)擊消息啟動app時的啟動圖
    // The launch image that will be used when the app is opened from the notification.
    open var launchImageName: String

    // 消息提示音,默認(rèn)是.default
    // The sound that will be played for the notification.
    @NSCopying open var sound: UNNotificationSound?

// 消息的payload內(nèi)容
  // Apps can set the userInfo for locally scheduled notification requests. The contents of the push payload will be set as the userInfo for remote notifications.
    open var userInfo: [AnyHashable : Any]


    // The unique identifier for the thread or conversation related to this notification request. It will be used to visually group notifications together.
    open var threadIdentifier: String


    /// The argument to be inserted in the summary for this notification.
    @available(iOS 12.0, *)
    open var summaryArgument: String

    
    /// A number that indicates how many items in the summary are represented in the summary.
    /// For example if a podcast app sends one notification for 3 new episodes in a show,
    /// the argument should be the name of the show and the count should be 3.
    /// Default is 1 and cannot be 0.
    @available(iOS 12.0, *)
    open var summaryArgumentCount: Int

UNNotificationTrigger 推送的觸發(fā)器

是一個抽象類,他有四個子類,代表四種不同的觸發(fā)方式

UNPushNotificationTrigger

遠(yuǎn)程通知觸發(fā),一般是由蘋果服務(wù)器觸發(fā)

UNTimeIntervalNotificationTrigger 時間間隔觸發(fā)器

一定時間間隔后觸發(fā)通知:

// 初始化方法,時間間隔,是否重復(fù)觸發(fā)
public convenience init(timeInterval: TimeInterval, repeats: Bool)
// 時間間隔,只讀
open var timeInterval: TimeInterval { get }

// 下次觸發(fā)的日期
    open func nextTriggerDate() -> Date?

例如:

// 10s后觸發(fā)
let timer = UNTimeIntervalNotificationTrigger(timeInterval: 10, repeats: false)
UNCalendarNotificationTrigger 日期時間觸發(fā)器

在某個日期的某個事件觸發(fā)通知

// 獲取當(dāng)前的DateComponents
open var dateComponents: DateComponents { get }

    // 初始化方法,參數(shù)是DateComponents類型
    // The next date is calculated using matching date components.
    public convenience init(dateMatching dateComponents: DateComponents, repeats: Bool)

    // 下次觸發(fā)的日期
    open func nextTriggerDate() -> Date?

例如:

// 每周日的8點(diǎn)
        var dateCom = DateComponents()
        dateCom.weekday = 1
        dateCom.hour = 8
        
        let calendarTri = UNCalendarNotificationTrigger(dateMatching: dateCom, repeats: true)

附,從DateComponents 獲取具體的時間


// 從 DateComponents 獲取具體的時間信息
        let calendar = Calendar(identifier: Calendar.Identifier.gregorian)
        
        if let date = calendar.date(from: dateCom) {
            let weekday = calendar.component(Calendar.Component.weekday, from: date)
        }
UNLocationNotificationTrigger 地理位置觸發(fā)器

當(dāng)進(jìn)入/離開某個地理范圍時,觸發(fā)的本地通知,需要有定位權(quán)限

@NSCopying open var region: CLRegion { get }

    // 初始化方法,參數(shù)為CLRegion
    public convenience init(region: CLRegion, repeats: Bool)

例如:

let center = CLLocationCoordinate2D(latitude: 37.33, longitude: -122.009)
        let regin = CLCircularRegion(center: center, radius: 2000, identifier: "reginid")
        regin.notifyOnExit = true
        regin.notifyOnEntry = true
        
        let locaTrig = UNLocationNotificationTrigger(region: regin, repeats: true)

通知快捷操作Action

UNNotificationAction

通知點(diǎn)擊事件的快捷操作,其創(chuàng)建主要是一個初始化方法

public convenience init(identifier: String, title: String, options: UNNotificationActionOptions = [])

open var identifier: String { get }
open var title: String { get }
open var options: UNNotificationActionOptions { get }

UNTextInputNotificationAction

通知快捷回復(fù)輸入框的action:

public convenience init(identifier: String, title: String, options: UNNotificationActionOptions = [], textInputButtonTitle: String, textInputPlaceholder: String)

open var textInputButtonTitle: String { get }
open var textInputPlaceholder: String { get }
UNNotificationActionOptions

快捷操作的設(shè)置選項(xiàng)

@available(iOS 10.0, *)
public struct UNNotificationActionOptions : OptionSet {

    public init(rawValue: UInt)

    // 需要授權(quán)驗(yàn)證才能響應(yīng),點(diǎn)擊不會打開app
    // Whether this action should require unlocking before being performed.
    public static var authenticationRequired: UNNotificationActionOptions { get }

    // 紅色的按鈕,點(diǎn)擊不會打開app
    // Whether this action should be indicated as destructive.
    public static var destructive: UNNotificationActionOptions { get }

    // 點(diǎn)擊后會打開app
    // Whether this action should cause the application to launch in the foreground.
    public static var foreground: UNNotificationActionOptions { get }
}

action的響應(yīng)事件會調(diào)用代理UNUserNotificationCenterDelegate的方法進(jìn)行反饋。

UNNotificationCategory

// identifier :當(dāng)前Category的唯一標(biāo)識符
// actions:需要展示的快捷按鈕集合
// intentIdentifiers:意圖標(biāo)識符,告訴系統(tǒng)該通知可能與Sari進(jìn)行的請求有關(guān)
// options:如何處理該消息
public convenience init(identifier: String, actions: [UNNotificationAction], intentIdentifiers: [String], options: UNNotificationCategoryOptions = [])

    // identifier :當(dāng)前Category的唯一標(biāo)識符
// actions:需要展示的快捷按鈕集合
// intentIdentifiers:意圖標(biāo)識符,告訴系統(tǒng)該通知可能與Sari進(jìn)行的請求有關(guān)
// hiddenPreviewsBodyPlaceholder:當(dāng)預(yù)覽被隱藏時,替換消息內(nèi)容body;例如開啟隱私保護(hù)的時候,鎖屏?xí)r看不到消息具體內(nèi)容
// options:如何處理該消息
    @available(iOS 11.0, *)
    public convenience init(identifier: String, actions: [UNNotificationAction], intentIdentifiers: [String], hiddenPreviewsBodyPlaceholder: String, options: UNNotificationCategoryOptions = [])

    // iOS 12中的消息分組
// identifier :當(dāng)前Category的唯一標(biāo)識符
// actions:需要展示的快捷按鈕集合
// intentIdentifiers:意圖標(biāo)識符,告訴系統(tǒng)該通知可能與Sari進(jìn)行的請求有關(guān)
// hiddenPreviewsBodyPlaceholder:當(dāng)預(yù)覽被隱藏時,替換消息內(nèi)容body;例如開啟隱私保護(hù)的時候,鎖屏?xí)r看不到消息具體內(nèi)容
// categorySummaryFormat:分組后的消息,顯示的第一則消息下面的摘要文字,默認(rèn)是“還有 %d 個通知”,可以自定義,以此參數(shù)傳入
// options:如何處理該消息
    @available(iOS 12.0, *)
    public convenience init(identifier: String, actions: [UNNotificationAction], intentIdentifiers: [String], hiddenPreviewsBodyPlaceholder: String?, categorySummaryFormat: String?, options: UNNotificationCategoryOptions = [])

UNNotificationCategory 創(chuàng)建完成后,其identifier 要賦值給UNMutableNotificationContent實(shí)例的categoryIdentifier屬性

UNNotificationAttachment 通知附加包

iOS 10之后,通知允許添加一個與該通知關(guān)聯(lián)的媒體文件,例如:一張圖片,一段音樂或者視頻,添加的文件必須保存在磁盤上。
對于本地通知,在添加通知的時候就需要創(chuàng)建UNNotificationAttachment實(shí)例,添加到相應(yīng)的content中;如果是遠(yuǎn)程通知,需要通過擴(kuò)展程序來下載附加的文件,然后創(chuàng)建UNNotificationAttachment實(shí)例,添加到通知中。

這里在創(chuàng)建 UNNotificationAttachment 實(shí)例的時候,會去校驗(yàn)文件的格式,如果是不支持的文件,或者超出規(guī)定大小的文件,會返回nil。

如果創(chuàng)建成功,文件數(shù)據(jù)將被移動到附件數(shù)據(jù)存儲中;如果是本地的文件,會復(fù)制文件數(shù)據(jù)到附件數(shù)據(jù)存儲中,以便于訪問這些數(shù)據(jù)。

支持的文件類型及大小限制

  • Audio 聲音文件 大小限制在 5Mb以內(nèi)
    支持的格式有kUTTypeAudioInterchangeFileFormat、kUTTypeWaveformAudio、kUTTypeMP3、kUTTypeMPEG4Audio
  • Image圖片 大小限制在 10Mb以內(nèi)
    支持的格式:kUTTypeJPEG、kUTTypeGIF、kUTTypePNG
  • Movie 視頻文件 最大 50Mb
    支持的格式:kUTTypeMPEG、kUTTypeMPEG2Video、kUTTypeMPEG4、kUTTypeAVIMovie

創(chuàng)建時,主要是有一個初始化方法:


 // Creates an attachment for the data at URL with an optional options dictionary. URL must be a file URL. Returns nil if the data at URL is not supported.
    public convenience init(identifier: String, url URL: URL, options: [AnyHashable : Any]? = nil) throws


// The identifier of this attachment
    open var identifier: String { get }

    // The URL to the attachment's data. If you have obtained this attachment from UNUserNotificationCenter then the URL will be security-scoped.
    open var url: URL { get }

    // The UTI of the attachment.
    open var type: String { get }

需要注意的是,這個初始化方法會拋出異常,需要使用try進(jìn)行判斷。

其參數(shù) options是一個字典,支持以下幾個key:

  • UNNotificationAttachmentOptionsTypeHintKey
    指定文件類型,其值為 String 類型,常用的有kUTTypeImage,kUTTypeJPEG2000,kUTTypeTIFF,kUTTypePICT,kUTTypeGIF ,kUTTypePNG,kUTTypeQuickTimeImage, 需要引入import MobileCoreServices
  • UNNotificationAttachmentOptionsThumbnailHiddenKey
    是否隱藏縮略圖,其值為 Number 類型的Bool
  • UNNotificationAttachmentOptionsThumbnailClippingRectKey
    縮略圖的裁剪區(qū)域,其值可以這樣創(chuàng)建 ,坐標(biāo)值為(0---1)
    let v = CGRect(x: 0, y: 0, width: 0.5, height: 0.5).dictionaryRepresentation
  • UNNotificationAttachmentOptionsThumbnailTimeKey
    動畫圖像幀數(shù)必須是NSNumber。視頻時間是一個以秒為單位的NSNumber,或者是使用CMTimeCopyAsDictionary編碼的CMTime

相關(guān)options介紹

UNNotificationCategoryOptions

告訴系統(tǒng)該如何處理該消息

@available(iOS 10.0, *)
public struct UNNotificationCategoryOptions : OptionSet {

    public init(rawValue: UInt)

    // 需要通過代理委托處理,調(diào)用UNUserNotificationCenterDelegate的相應(yīng)方法
    // Whether dismiss action should be sent to the UNUserNotificationCenter delegate
    public static var customDismissAction: UNNotificationCategoryOptions { get }

    // 允許在駕駛模式下顯示通知
    // Whether notifications of this category should be allowed in CarPlay
    public static var allowInCarPlay: UNNotificationCategoryOptions { get }

    // 在用戶禁止顯示預(yù)覽的情況下,顯示標(biāo)題
    // Whether the title should be shown if the user has previews off
    @available(iOS 11.0, *)
    public static var hiddenPreviewsShowTitle: UNNotificationCategoryOptions { get }
// 在用戶禁止顯示預(yù)覽的情況下,顯示副標(biāo)題
    // Whether the subtitle should be shown if the user has previews off
    @available(iOS 11.0, *)
    public static var hiddenPreviewsShowSubtitle: UNNotificationCategoryOptions { get }
}
UNAuthorizationOptions

請求權(quán)限的類型,請求權(quán)限的方法 requestAuthorization 參數(shù)

@available(iOS 10.0, *)
public struct UNAuthorizationOptions : OptionSet {

    public init(rawValue: UInt)

    // 角標(biāo)提醒
    public static var badge: UNAuthorizationOptions { get }
// 聲音提醒
    public static var sound: UNAuthorizationOptions { get }
// 彈框提醒
    public static var alert: UNAuthorizationOptions { get }
// 行車模式下,接收通知
    public static var carPlay: UNAuthorizationOptions { get }

    @available(iOS 12.0, *)
    public static var criticalAlert: UNAuthorizationOptions { get }

    @available(iOS 12.0, *)
    public static var providesAppNotificationSettings: UNAuthorizationOptions { get }

    @available(iOS 12.0, *)
    public static var provisional: UNAuthorizationOptions { get }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容