iOS10新增加了一個(gè)UserNotificationKit(用戶通知框架)來(lái)整合通知相關(guān)的API,UserNotificationKit框架增加了很多令人驚喜的特性:
- 更加豐富的推送內(nèi)容:現(xiàn)在可以設(shè)置推送的title局骤、subtitle虾宇、body 以及符合大小的圖片、音頻队秩、視頻等附件內(nèi)容笑旺。
- 更好的通知管理:現(xiàn)在可以對(duì)通知進(jìn)行查看、更新馍资、刪除了筒主。
- 更優(yōu)雅的展示方式:可以設(shè)置應(yīng)用在前臺(tái)展示通知。
對(duì)iOS10而言鸟蟹,UserNotificationKit(用戶通知框架)是一種系統(tǒng)層面的UI展示乌妙,遠(yuǎn)程通知APNS只是通知的一種觸發(fā)方式,UserNotificationKit的重要意義在于統(tǒng)一了Remote(遠(yuǎn)程通知)和Local(本地通知)建钥。
UserNotificationKit 基本流程
UserNotificationKit使用大概分為以下幾個(gè)過(guò)程:
- 注冊(cè)通知: 獲取相關(guān)權(quán)限藤韵,注冊(cè)APNS
- 發(fā)送通知: 創(chuàng)建通知并發(fā)起通知請(qǐng)求
- 處理通知: 處理通知回調(diào),對(duì)通知進(jìn)行額外的操作
首先你需要向用戶請(qǐng)求通知權(quán)限,在取得權(quán)限后注冊(cè)通知;
然后你可以創(chuàng)建一個(gè)通知并發(fā)起推送,當(dāng)然對(duì)于遠(yuǎn)程推送APNS而言熊经,你還需要注冊(cè)DeviceToken;
在接受到推送通知后可以根據(jù)app的運(yùn)行情況決定是否展示通知泽艘,當(dāng)然你也可以通過(guò)一系列的回調(diào)接口對(duì)通知進(jìn)行處理加工。
注冊(cè)通知
請(qǐng)求權(quán)限
iOS 10 進(jìn)一步消除了本地推送和遠(yuǎn)程推送的區(qū)別镐依,因?yàn)閮烧咴谏暾?qǐng)權(quán)限的時(shí)候都是在打斷用戶的行為匹涮。因此iOS 10統(tǒng)一了推送權(quán)限的申請(qǐng):
//iOS8以下
[application registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound];
//iOS8 - iOS10
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge categories:nil]];
//iOS10
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
UNAuthorizationOptions options = UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert;
[center requestAuthorizationWithOptions:options completionHandler:^(BOOL granted, NSError * _Nullable error) {
}
//iOS10 swift
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]{ granted, error in
}
推送設(shè)置
iOS 10 還可以實(shí)時(shí)獲取用戶當(dāng)前的推送的設(shè)置:
//可以獲取的設(shè)置參數(shù)
@available(iOS 10.0, *)
open class UNNotificationSettings : NSObject, NSCopying, NSSecureCoding {
open var authorizationStatus: UNAuthorizationStatus { get }
open var soundSetting: UNNotificationSetting { get }
open var badgeSetting: UNNotificationSetting { get }
open var alertSetting: UNNotificationSetting { get }
open var notificationCenterSetting: UNNotificationSetting { get }
open var lockScreenSetting: UNNotificationSetting { get }
open var carPlaySetting: UNNotificationSetting { get }
open var alertStyle: UNAlertStyle { get }
}
//獲取設(shè)置
UNUserNotificationCenter.current().getNotificationSettings {
settings in
print(settings.authorizationStatus) // .authorized | .denied | .notDetermined
print(settings.badgeSetting) // .enabled | .disabled | .notSupported
}
在有些情況下,可以對(duì)推送權(quán)限設(shè)置進(jìn)行檢查槐壳。
APNS:DeviceToken
一旦獲得用戶權(quán)限后然低,你就可以在應(yīng)用中發(fā)送本地通知了。不過(guò)對(duì)于APNS而言务唐,你還需要申請(qǐng)DeviceToken雳攘,把DeviceToken傳遞給服務(wù)器,然后服務(wù)器根據(jù)DeviceToken發(fā)送到對(duì)應(yīng)的用戶枫笛。
//向APNSS請(qǐng)求deviceToken:
UIApplication.shared.registerForRemoteNotifications()
//回調(diào)
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
print("Get Push token: \(deviceToken)")
}
發(fā)送通知
本地通知發(fā)送的基本流程
1.設(shè)置推送內(nèi)容
2.設(shè)置通知觸發(fā)器
3.添加通知
1.設(shè)置推送內(nèi)容
let content = UNMutableNotificationContent()
content.title = "Time Interval Notification" //推送內(nèi)容標(biāo)題
content.subtitle = "My first notification" //推送內(nèi)容子標(biāo)題
content.body = "My first notification"http:// 推送內(nèi)容body
content.categoryIdentifier = "categoryIdentifier" //category標(biāo)識(shí)吨灭,操作策略
2.設(shè)置通知觸發(fā)器
有4種觸發(fā)器
UNPushNotificationTrigger 觸發(fā)APNS服務(wù),系統(tǒng)自動(dòng)設(shè)置(這是區(qū)分本地通知和遠(yuǎn)程通知的標(biāo)識(shí))
UNTimeIntervalNotificationTrigger 一段時(shí)間后觸發(fā)
UNCalendarNotificationTrigger 指定日期觸發(fā)
UNLocationNotificationTrigger 根據(jù)位置觸發(fā)刑巧,支持進(jìn)入某地或者離開(kāi)某地或者都有沃于。
//設(shè)置5秒后觸發(fā)
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
//每周三,13點(diǎn)觸發(fā)
var components:DateComponents = DateComponents()
components.weekday = 4;//周三
components.hour = 13;//13點(diǎn)
let triggerDateComponents = UNCalendarNotificationTrigger(dateMatching: components, repeats: true);
//這個(gè)點(diǎn)海诲,100米范圍內(nèi)繁莹,進(jìn)入觸發(fā)。
let Coordinate2:CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 39, longitude: 116);
let region:CLCircularRegion = CLCircularRegion(center: Coordinate2, radius: 100, identifier: "center")
region.notifyOnEntry = true;
region.notifyOnExit = false;
let triggerRegion = UNLocationNotificationTrigger(region: region, repeats: true);
3.添加通知
//先創(chuàng)建一個(gè)請(qǐng)求
let request = UNNotificationRequest(identifier: requestIdentifier, content: content, trigger: trigger)
//將請(qǐng)求添加到發(fā)送中心
UNUserNotificationCenter.current().add(request) { error in
if error == nil {
print("Time Interval Notification scheduled: \(requestIdentifier)")
}
}
APNS:Payload
iOS 10之前的推送內(nèi)容比較單一特幔,僅僅支持一段文字的消息推送咨演,最好帶上badge和sound,以及一些自定義的字段內(nèi)容蚯斯,而在iOS 10中薄风,Apple豐富了推送內(nèi)容饵较,并且增加了更多的顯示方式。
//iOS 10 之前的基礎(chǔ)Payload:
{
"aps":{
"alert":{
"body": "This is a message"
},
"sound":"default",
"badge":1
}
}
//iOS 10 基礎(chǔ)Payload:
{
"aps":{
"alert":{
"title":"I am title",
"subtitle":"I am subtitle",
"body":"I am body"
},
"sound":"default",
"badge":1
}
}
title和subtitle并不是必須的遭赂。
處理通知
常規(guī)處理
在創(chuàng)建通知請(qǐng)求時(shí)循诉,我們已經(jīng)指定了標(biāo)識(shí)符。iOS10中我們可以通過(guò)標(biāo)識(shí)符來(lái)管理處理通知撇他。通UserNotificationKIt的一系列API 你可以做到: 查找茄猫、更新和刪除通知。其中關(guān)鍵在于request的identifier困肩,即在創(chuàng)建的時(shí)候的指定的通知的標(biāo)識(shí)符划纽。
查找通知
//獲取所有等待遞送的通知
UNUserNotificationCenter.current().getPendingNotificationRequests { (request) in
}
//獲取所有已經(jīng)遞送的通知
UNUserNotificationCenter.current().getDeliveredNotifications { (noti) in
}
更新通知
遠(yuǎn)程推送可以進(jìn)行通知的更新,在使用 center 的 addNotificationRequest:withCompletionHandler: 向 APNS提交請(qǐng)求時(shí)锌畸,在 HTTP/2 的 header 中 apns-collapse-id key 的內(nèi)容將被作為該推送的標(biāo)識(shí)符進(jìn)行使用勇劣。多次推送同一標(biāo)識(shí)符的通知即可進(jìn)行更新。
//先創(chuàng)建一個(gè)請(qǐng)求
let request = UNNotificationRequest(identifier: requestIdentifier, content: content, trigger: trigger)
//重新添加通知
UNUserNotificationCenter.current().add(request) { error in
if error == nil {
print("Time Interval Notification scheduled: \(requestIdentifier)")
}
}
刪除通知/取消通知
//使用這個(gè)API來(lái)移除通知 還有一系列相似的API
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: dentifiers)
//取消還未展示的本地通知
UNUserNotificationCenter.current().add(request) { error in
if error != nil {
print("Notification request added: \(identifier)")
}
}
delay(2) {
let newTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
let newRequest = UNNotificationRequest(identifier: identifier, content:newContent, trigger: newTrigger)
UNUserNotificationCenter.current().add(newRequest) { error in
if error != nil {
print("Notification request updated: \(identifier)")
}
}
}
通知的回調(diào):UNUserNotificationCenterDelegate
UNUserNotificationCenterDelegate提供了兩個(gè)方法處理通知潭枣。兩個(gè)方法分別對(duì)應(yīng)如何在應(yīng)用內(nèi)展示通知比默,和收到通知響應(yīng)時(shí)要如何處理的工作。
//在應(yīng)用內(nèi)展示通知
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
completionHandler([.alert, .sound])
// 如果不想顯示某個(gè)通知盆犁,可以直接用空 options 調(diào)用 completionHandler:
// completionHandler([])
}
//對(duì)通知進(jìn)行響應(yīng)益楼,收到通知響應(yīng)時(shí)的處理工作折柠,用戶與你推送的通知進(jìn)行交互時(shí)被調(diào)用楣铁;
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
completionHandler()
}
Notification Extension
iOS 10 中添加了很多 extension癌瘾,作為應(yīng)用與系統(tǒng)整合的入口瓮下。與通知相關(guān)的 extension 有兩個(gè):Service Extension 和 Content Extension翰铡。前者可以讓我們有機(jī)會(huì)在「接收到推送之后、展示推送之前」對(duì)通知內(nèi)容進(jìn)行修改讽坏;后者可以用來(lái)自定義通知視圖的樣式锭魔。
Service Extension
Service Extension可以對(duì)推送進(jìn)行處理,更改路呜、替換原有的內(nèi)容迷捧。他可以對(duì)通知內(nèi)容進(jìn)行加密,也可以給推送展示內(nèi)容添加附件(比如照片胀葱、背景音樂(lè))漠秋,使得內(nèi)容更加豐富。
//NotificationService 的模板已經(jīng)為我們進(jìn)行了基本的實(shí)現(xiàn):
class NotificationService: UNNotificationServiceExtension {
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
// 1 讓你可以在后臺(tái)處理接收到的推送抵屿,傳遞修改后的的內(nèi)容給 contentHandler,然后展示
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
if let bestAttemptContent = bestAttemptContent {
if request.identifier == "mutableContent" {
bestAttemptContent.body = "\(bestAttemptContent.body), onevcat"
}
contentHandler(bestAttemptContent)
}
}
// 2 在在你獲得的一小段運(yùn)行代碼的時(shí)間內(nèi)沒(méi)有調(diào)用 contentHandler 的話庆锦,系統(tǒng)會(huì)調(diào)用這個(gè)方法≡穑可以在這里傳肯定不會(huì)出錯(cuò)的內(nèi)容搂抒,或者他會(huì)默認(rèn)傳遞原始的推送內(nèi)容艇搀。
override func serviceExtensionTimeWillExpire() {
if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
contentHandler(bestAttemptContent)
}
}
}
Service Extension 現(xiàn)在只對(duì)遠(yuǎn)程推送APNS的通知起效,你可以在推送 payload 中增加一個(gè) mutable-content 值為 1 的項(xiàng)來(lái)啟用內(nèi)容修改
{
aps : {
alert : "New Message",
mutable-content : 1
},
encrypted-content : "#myencryptedcontent"
}
Media Attachments
iOS 10 的另一個(gè)亮眼功能是多媒體的推送求晶,開(kāi)發(fā)者能夠直接在推送的通知內(nèi)容里面?zhèn)鬟f多媒體資源焰雕,在客戶端進(jìn)行直接的展示,極大豐富了推送內(nèi)容的可讀性和趣味性芳杏。
為本地通知添加添加多媒體內(nèi)容
通過(guò)本地磁盤(pán)上的文件 URL 創(chuàng)建一個(gè) UNNotificationAttachment 對(duì)象矩屁,然后將這個(gè)對(duì)象放到數(shù)組中賦值給 content 的 attachments 屬性:
let content = UNMutableNotificationContent()
content.title = "Hey guys"
content.body = " body "
if let imageURL = Bundle.main.url(forResource: "image", withExtension: "jpg"),
let attachment = try? UNNotificationAttachment(identifier: "imageAttachment", url: imageURL, options: nil)
{
content.attachments = [attachment]
}
為遠(yuǎn)程推送添加多媒體內(nèi)容
只需要在推送的 payload 中指定需要加載的資源地址,這個(gè)地址可以是應(yīng)用 bundle 內(nèi)已經(jīng)存在的資源蚜锨,也可以是網(wǎng)絡(luò)的資源档插。不過(guò)因?yàn)樵趧?chuàng)建 UNNotificationAttachment 時(shí)我們只能使用本地資源,所以如果多媒體還不在本地的話亚再,我們需要先將其下載到本地郭膛。在完成 UNNotificationAttachment 創(chuàng)建后,我們就可以和本地通知一樣氛悬,將它設(shè)置給 attachments 屬性则剃,然后調(diào)用 contentHandler 了。
{
"aps":{
"alert":{
"title":"Image Notification",
"body":"Show me an image from web!"
},
"mutable-content":1 //表示我們會(huì)在接收到通知時(shí)對(duì)內(nèi)容進(jìn)行更改
},
"image": "https://img1.baidu.com.jpg"http://指明了目標(biāo)圖片的地址
}
- 如果你想在遠(yuǎn)程推送來(lái)的通知中顯示應(yīng)用 bundle 內(nèi)的資源的話如捅,要注意 extension 的 bundle 和 app main bundle 并不是一回事兒棍现。你可以選擇將圖片資源放到 extension bundle 中,也可以選擇放在 main bundle 里镜遣〖喊梗總之,你需要保證能夠獲取到正確的悲关,并且你具有讀取權(quán)限的 url谎僻。
- UNNotificationContent 的 attachments 雖然是一個(gè)數(shù)組,但是系統(tǒng)只會(huì)展示第一個(gè) attachment 對(duì)象的內(nèi)容寓辱。不過(guò)你依然可以發(fā)送多個(gè) attachments艘绍,然后在要展示的時(shí)候再重新安排它們的順序,以顯示最符合情景的圖片或者視頻秫筏。
Notification Actions
iOS 8 和 9 中 Apple 引入了可以交互的通知诱鞠,這是通過(guò)將一簇 action 放到一個(gè) category 中,將這個(gè) category 進(jìn)行注冊(cè)这敬,最后在發(fā)送通知時(shí)將通知的 category 設(shè)置為要使用的 category 來(lái)實(shí)現(xiàn)的航夺。這些action的API在iOS10 中被統(tǒng)一。
注冊(cè)Actions
private func registerNotificationCategory() {
let saySomethingCategory: UNNotificationCategory = {
// 1 創(chuàng)建 action 即一項(xiàng)交互操作
let inputAction = UNTextInputNotificationAction(
identifier: "action.input",
title: "Input", // 是交互按鈕的內(nèi)容
options: [.foreground], // options 可以讓該 action 成為一條可在前臺(tái)執(zhí)行的 action
textInputButtonTitle: "Send",
textInputPlaceholder: "What do you want to say...")
let goodbyeAction = UNNotificationAction(
identifier: "action.goodbye",//崔涂,推送消息的其中的identifier
title: "Goodbye",
options: [.foreground])
let cancelAction = UNNotificationAction(
identifier: "action.cancel",
title: "Cancel",
options: [.destructive])
//2. 創(chuàng)建 category
return UNNotificationCategory(identifier:"saySomethingCategory", actions: [inputAction, goodbyeAction, cancelAction], intentIdentifiers: [], options: [.customDismissAction])
}()
// 3 把 category 添加到通知中心:
UNUserNotificationCenter.current().setNotificationCategories([saySomethingCategory])
}
// 4 觸發(fā)方式
//遠(yuǎn)程通知觸發(fā)方式
{
aps : {
alert : "Welcome to WWDC !",
category : "saySomethingCategory" //和第二步identifier:"saySomethingCategory一致
}
}
//本地通知觸發(fā)方式 Local Notifications 只需要在創(chuàng)建 contnet 的時(shí)候指定 和第二步identifier 即可
content.categoryIdentifier = "saySomethingCategory";
通過(guò)上面的三個(gè)步驟我們已經(jīng)創(chuàng)建了一個(gè)category
作者:曲年
鏈接:http://www.reibang.com/p/567abab2098f
來(lái)源:簡(jiǎn)書(shū)
著作權(quán)歸作者所有阳掐。商業(yè)轉(zhuǎn)載請(qǐng)聯(lián)系作者獲得授權(quán),非商業(yè)轉(zhuǎn)載請(qǐng)注明出處。