iOS 10 came
在今年 6月14號 蘋果開發(fā)者大會 WWDC 2016 之后,筆者趕緊就去 apple 的開發(fā)者網(wǎng)站下載了最新的 Xcode 8 beta 和 iOS 10 beta,然后在自己的手機上裝了 iOS 10 beta ,狠狠地體驗了一把。
可以說 iOS 10 無論從界面風格,還是 Framework 都做了很多改動。最直觀的感受就是界面的圓角增多了,系統(tǒng)動畫更加多樣和流暢,系統(tǒng) App 的功能也變得更豐富了。
而 iOS 10 里的推送功能,也較之前更加強大,
今天我們就來聊聊 iOS 10 里的推送功能。
Notifications before iOS 10
首先我們一起簡單回顧下 iOS 10 以前的推送服務(wù)。
iOS 推送分為 Local Notifications(本地推送) 和 Remote Notifications(遠程推送),先看 2 張圖:
簡單的說就是本地推送通過 App 本地定制,加入到系統(tǒng)的 Schedule 里,然后在指定的時間推送指定文字。而遠程推送通過服務(wù)端向蘋果推送服務(wù)器 Apple Push Notification Service (APNs) 發(fā)送 Notification Payload,之后 APNs 再將推送下發(fā)到指定設(shè)備的 指定 App 上。
以及 iOS 7 之后在不顯式地彈窗打擾用戶的情況下,進行的靜默推送:
具體做法可以參考 iOS 7 Background Remote Notification
User Notifications Framework
好,扯了這么多,該進入今天的正題了 —— User Notifications Framework 。
首先在 AppDelegate.m
中
import
#import <UserNotifications/UserNotifications.h>
注冊推送
以下分別是 iOS 10 之前和之后的注冊方式,其中的 UNAuthorizationOptions
里還可以找到 1 個 UNAuthorizationOptionCarPlay
的值是專為車載系統(tǒng)定制的值。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//iOS 10 before
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
[application registerUserNotificationSettings:settings];
//iOS 10
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (!error) {
NSLog(@"request authorization succeeded!");
}
}];
return YES;
}
Notification settings
之前注冊推送服務(wù),ios 8 及之前使用了不同的 API,并且返回結(jié)果也不同。現(xiàn)在 apple 不僅統(tǒng)一了這個 API,而且我們可以獲取到用戶更加詳細的設(shè)定了。
[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
NSLog(@"%@",settings);
}];
打印獲得如下信息:
<UNNotificationSettings: 0x16567310;
authorizationStatus: Authorized,
notificationCenterSetting: Enabled,
soundSetting: Enabled,
badgeSetting: Enabled,
lockScreenSetting: Enabled,
alertSetting: NotSupported,
carPlaySetting: Enabled,
alertStyle: Banner>
Token Registration
跟之前一樣
[[UIApplication sharedApplication] registerForRemoteNotifications];
Content
以前只能展示一條文字,現(xiàn)在可以有 title 、subtitle 以及 body 了。
定制方法如下:
//Local Notification
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
content.title = @"Introduction to Notifications";
content.subtitle = @"Session 707";
content.body = @"Woah! These new notifications look amazing! Don’t you agree?";
content.badge = @1;
//Remote Notification
{
"aps" : {
"alert" : {
"title" : "Introduction to Notifications",
"subtitle" : "Session 707",
"body" : "Woah! These new notifications look amazing! Don’t you agree?"
},
"badge" : 1
},
}
Triggers
又是一個新的功能,有三種
- UNTimeIntervalNotificationTrigger
- UNCalendarNotificationTrigger
- UNLocationNotificationTrigger
//2 分鐘后提醒
UNTimeIntervalNotificationTrigger *trigger1 = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:120 repeats:NO];
//每小時重復 1 次喊我喝水
UNTimeIntervalNotificationTrigger *trigger2 = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:3600 repeats:YES];
//每周一早上 8:00 提醒我給老婆做早飯
NSDateComponents *components = [[NSDateComponents alloc] init];
components.weekday = 2;
components.hour = 8;
UNCalendarNotificationTrigger *trigger3 = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:YES];
//#import <CoreLocation/CoreLocation.h>
//一到麥當勞就喊我下車
CLRegion *region = [[CLRegion alloc] init];
UNLocationNotificationTrigger *trigger4 = [UNLocationNotificationTrigger triggerWithRegion:region repeats:NO];
Add Request
NSString *requestIdentifier = @"sampleRequest";
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:requestIdentifier
content:content
trigger:trigger1];
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
}];
推送小結(jié)
然后整個推送的過程就變成了醬紫:
- Local Notifications 通過定義
Content
和Trigger
向UNUserNotificationCenter
進行request
這三部曲來實現(xiàn)。 - Remote Notifications 則向
APNs
發(fā)送Notification Payload
。
Notification Handling
設(shè)定了推送,然后就結(jié)束了?iOS 10 并沒有這么簡單!
通過實現(xiàn)協(xié)議,使 App 處于前臺時捕捉并處理即將觸發(fā)的推送:
@interface AppDelegate () <UNUserNotificationCenterDelegate>
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
completionHandler(UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionSound);
}
讓它只顯示 alert 和 sound ,而忽略 badge 。
Notification Management
徹底掌控整個推送周期:
- Local Notification 通過更新 request
- Remote Notification 通過新的字段
apns-collapse-id
通過之前的 addNotificationRequest:
方法,在 id
不變的情況下重新添加,就可以刷新原有的推送。
NSString *requestIdentifier = @"sampleRequest";
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:requestIdentifier
content:newContent
trigger:newTrigger1];
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
}];
刪除計劃的推送:
[center removePendingNotificationRequestsWithIdentifiers:@[requestIdentifier]];
此外 UNUserNotificationCenter.h
中還有諸如刪除所有推送、查看已經(jīng)發(fā)出的推送、刪除已經(jīng)發(fā)出的推送等等強大的接口。
刷新原有的推送后,在通知中心的顯示里,也會有相應(yīng)的變化,這里注意第 2 條信息,現(xiàn)在比分是 1:0
比分刷新后為 1:1,在不產(chǎn)生新的推送條目的情況下位置被前置了!
試想利用這個方法,不斷的刷新推送,是不是就可以做到讓自己 App 的推送內(nèi)容始終展示在用戶手機通知中心的最頂端,力壓其余所有內(nèi)容了呢?總感覺有點不厚道啊~
Advanced Notifications
關(guān)于推送的更多類似 Media Attachments
的高級功能,我們將在下一篇里詳細討論。
Media Attachments
為推送添加更多媒體附件,諸如圖片、音樂
Notification Actions
在 iOS 10 中,可以允許推送添加交互操作 action
,這些 action
可以使得 App 在前臺或后臺執(zhí)行一些邏輯代碼。并且在鎖屏界面通過 3d-touch 觸發(fā)。如:推出鍵盤進行快捷回復,該功能以往只在 iMessage 中可行。
(Notification Actions 在 iOS 8 引入,快捷回復在 iOS 9 引入,在 iOS 10 中,這些 API 被統(tǒng)一。)
在 iOS 10 中,這叫 category
,是對推送功能的一個拓展,可以通過 3d-touch 觸發(fā)。
-
創(chuàng)建
action
即一項交互操作
title
是交互按鈕的內(nèi)容options
可以讓該action
成為一條可在前臺執(zhí)行的action
-
創(chuàng)建:
UNNotificationAction *action = [UNNotificationAction actionWithIdentifier:@"reply" title:@"Reply" options:UNNotificationActionOptionNone];
-
創(chuàng)建
category
可添加多個
action
的數(shù)組,就像圖片中一樣,有多種操作其中的
id
,需要填寫你想要添加到哪個推送消息的id
-
創(chuàng)建:
UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"message" actions:@[action] minimalActions:@[action] intentIdentifiers:@[] options:UNNotificationCategoryOptionNone];
-
把
category
添加到通知中心:[[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithArray:@[category]]];
-
觸發(fā)方式:
-
Remote Notifications 配置 payload,指定其中 category 的值與第 2 步中 Identifier 一致:
{ aps : { alert : "Welcome to WWDC !", category : "message" } }
-
Local Notifications 只需要在創(chuàng)建 contnet 的時候指定 Id 即可:(content 相關(guān)內(nèi)容請參照 上一篇 中的 Content 部分)
content。categoryIdentifier = @"message";
?
-
Dismiss Actions
鎖屏及在通知中心收到推送,側(cè)滑,會展示 action。
只要點擊 Clear 就可以將該條推送清除,并且重復的內(nèi)容不會被發(fā)送到你的其他 iOS 設(shè)備上。
跟 Notification Actions 只有一點小區(qū)別,就是添加 action 到 category 的時候,增加一個 option 的值 UNNotificationCategoryOptionCustomDismissAction:
UNNotificationAction *clearAction = [UNNotificationAction actionWithIdentifier:@"clear" title:@"clear" options:UNNotificationActionOptionNone];
UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"clear" actions:@[clearAction] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];//這里增加一個 dismiss 的值
Response handling
用戶點擊這些 actions 以后,是啟動 App、觸發(fā)鍵盤、清除通知或是有其他的響應(yīng),這些全部只需要實現(xiàn)協(xié)議 UNUserNotificationCenterDelegate 中的一個方法就可以控制:
@interface ClassName () <UNUserNotificationCenterDelegate>
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{
}
其中的 response 包含以下內(nèi)容:
其中的 trigger 可以用來判斷是遠程推送還是本地推送。
處理 response 舉例:
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{
NSString *categoryIdentifier = response.notification.request.content.categoryIdentifier;
if ([categoryIdentifier isEqualToString:@"handle category"]) {//識別需要被處理的拓展
if ([response.actionIdentifier isEqualToString:@"input text"]) {//識別用戶點擊的是哪個 action
//假設(shè)點擊了輸入內(nèi)容的 UNTextInputNotificationAction 把 response 強轉(zhuǎn)類型
UNTextInputNotificationResponse *textResponse = (UNTextInputNotificationResponse*)response;
//獲取輸入內(nèi)容
NSString *userText = textResponse.userText;
//發(fā)送 userText 給需要接收的方法
[ClassName handleUserText: userText];
}else{
}
}
completionHandler();
}
Service Extension
可以在手機「接收到推送之后、展示推送之前」對推送進行處理,更改、替換原有的內(nèi)容。
使用了這個玩意,你們公司原有發(fā)送推送的 payload 可以完全不變,而在客戶端對接收到的內(nèi)容(只有一條字符串)進行加工,從而適配 iOS 10 的展示效果(標題+副標題+內(nèi)容)。
「接收到推送之后、展示推送之前」:
- 此時,你獲得了一小段在后臺運行代碼的時間(也可以用來干別的壞事>。<,可以偷偷的斷點下載你們 App 的更新包)
- 而如果你更改推送內(nèi)容出了錯誤,或者你調(diào)用什么方法失敗了,那么最終會正常的展示最初接收到的推送內(nèi)容。
Potential uses
值得你們 App 充分發(fā)揮的是可以做以下事情:
- 端到端加密
- 給推送展示內(nèi)容添加附件(比如照片、背景音樂),使得內(nèi)容更加豐富,就像從推送里拉出了一個網(wǎng)頁有木有!
不急,我們先來介紹怎么
添加 Service Extension
先在 Xcode 打開你的 App 工程,F(xiàn)ile - New - Target 然后添加這個:
然后會自動創(chuàng)建一個 UNNotificationServiceExtension 的子類 NotificationService,通過完善這個子類,來實現(xiàn)你的需求。
點開 NotificationService.m 會看到 2 個方法:
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
self.contentHandler = contentHandler;
self.bestAttemptContent = [request.content mutableCopy];
self.bestAttemptContent.title = [NSString stringWithFormat:@"%@ [modified]", self.bestAttemptContent.title];
self.contentHandler(self.bestAttemptContent);
}
- (void)serviceExtensionTimeWillExpire {
self.contentHandler(self.bestAttemptContent);
}
- didReceiveNotificationRequest 讓你可以在后臺處理接收到的推送,傳遞最終的內(nèi)容給 contentHandler
- serviceExtensionTimeWillExpire 在你獲得的一小段運行代碼的時間即將結(jié)束的時候,如果仍然沒有成功的傳入內(nèi)容,會走到這個方法,可以在這里傳肯定不會出錯的內(nèi)容,或者他會默認傳遞原始的推送內(nèi)容
Example payload
{
aps : {
alert : "New Message",
mutable-content : 1
},
encrypted-content : "#myencryptedcontent"
}
首先需要添加 mutable-content : 1,這意味著此條推送可以被 Service Extension 進行更改
同時可以附加一條 encrypted-content,可以提取該內(nèi)容進行替換
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
//用你的重編碼方法對該內(nèi)容進行更改
NSString *decryptedBody = [DecryptClass decrypt: request.content.userInfo[@"encrypted-content"]];
//創(chuàng)建新的 content 并添加修改過的 body
UNMutableNotificationContent *newContent = [UNMutableNotificationContent new];
newContent.body = decryptedBody;
//回調(diào)新的 content
contentHandler(newContent);
}
Notifications User Interface
我們先來看一下 iOS 10 默認的推送 UI。
包括「橫幅、鎖屏、通知中心 」三處,看起來差不多的樣子。
Media Attachments
推送內(nèi)容中增圖片、gif、audio、video。
在以上的三個界面都可以通過 3d-touch 觸發(fā)。
先一起來看看效果。
添加方法
打開 iOS Xcode Project - File - New - Target - iOS - Notification Service Extension - Next - Product Name 填寫
yourPushNotificationService
- Finish
具體圖示方法,在《中》里有詳細的介紹。-
添加文件。把你們定制的各種 media 文件拖拽至上一步系統(tǒng)自動生成的
yourPushNotificationService
文件夾下,勾上 copy to items,add to targets 一定要選擇這個 Notification Service 作為 target,如下所示。
添加代碼。在 2 中生成的
NotificationService.m
里添加代碼:
-(void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
self.contentHandler = contentHandler;
// 1.把推送內(nèi)容轉(zhuǎn)為可變類型
self.bestAttemptContent = [request.content mutableCopy];
// 2.獲取 1 中自定義的字段 value
NSString *urlStr = [request.content.userInfo valueForKey:@"your-attachment"];
// 3.將文件夾名和后綴分割
NSArray *urls = [urlStr componentsSeparatedByString:@"."];
// 4.獲取該文件在本地存儲的 url
NSURL *urlNative = [[NSBundle mainBundle] URLForResource:urls[0] withExtension:urls[1]];
// 5.依據(jù) url 創(chuàng)建 attachment
UNNotificationAttachment *attachment = [UNNotificationAttachment attachmentWithIdentifier:urlStr URL:urlNative options:nil error:nil];
// 6.賦值 @[attachment] 給可變內(nèi)容
self.bestAttemptContent.attachments = @[attachment];
// 7.處理該內(nèi)容
self.contentHandler(self.bestAttemptContent);
}
先運行你的項目 target 使之在手機上安裝,再運行 Notification Service 的 target,并選擇在你的項目上運行該 Extension。此時可進行 Notification Service 代碼的調(diào)試,即在
NotificationService.m
中打斷點可以調(diào)試,但是在你的項目中的斷點無法調(diào)試。發(fā)送 payload 需依照下述格式:
{
aps : {
alert : {...},
mutable-content : 1 //必須
}
your-attachment : aPicture.png //必須
}
其中:
-
mutable-content : 1
說明該推送在接收后可被修改,這個字段決定了系統(tǒng)是否會調(diào)用 Notification Service 中的方法。 - your-attachment:是自定義的字段,key 可以自定義(你自己要記?。?,value 需要是一個完整的文件名(或 url,后續(xù)會詳細解釋),即你想要展示的文件。
手機接收后,在任一個能看到推送條目的界面對推送條目進行 3d-touch 強按都可以觸發(fā)。(需要 iPhone 6s 及以后設(shè)備 & iOS 10)
提示:各種 media 文件大小有一定限制,圖片、視頻等過大都不會被展示,Apple 的意思是:對于圖片,最大寬度也就和屏幕等寬,過大的圖片沒有意義;對于音頻、視頻等,完全可以提供一個短時間預覽部分,更多的內(nèi)容還是需要用戶點擊推送進入 App 之后對完整的內(nèi)容進行查看。希望開發(fā)者遵從這樣的邏輯進行開發(fā)。
Notification Content
iOS 10 新增的另一項 Extension,用于完全自定義推送展示的 UI 界面,響應(yīng) Actions 的同時刷新該 UI。簡單的說就是你可以把需要推送的內(nèi)容(比如一條完整的新聞快訊,包括多條文字+圖片的組合)全部放到一條推送里,用戶點擊了一個 Action(如贊、踩、關(guān)注、甚至評論等),在推送里立刻刷新 UI(如展示加星動畫、評論內(nèi)容等)。
特點
- 需要添加 Notification content extension
- 完全自定義 UI
- 推送 UI 不能響應(yīng)觸摸、點擊、滑動等任何手勢
- 可以響應(yīng) notification actions
下圖中日程表的 UI 完全由開發(fā)者自定義,并且在點擊了 Accept 之后,UI 立即發(fā)生了變化:
添加方法
打開 iOS Xcode Project - File - New - Target - iOS - Notification Content - Next - Product Name 填寫 yourPushNotificationContent
- Finish
系統(tǒng)會在 Xcode 工程目錄中 自動生成 yourPushNotificationContent
文件夾,并且包含四個文件:NotificationViewController.h
、NotificationViewController.m
、MainInterface.storyboard
、Info.plist
。
NotificationViewController 繼承自 UIViewController,并實現(xiàn)了 UNNotificationContentExtension 協(xié)議。
MainInterface.storyboard
拖拖拽拽一個 UI 就出來了 。
NotificationViewController.h/m
- 你可以在 viewDidLoad 里各種代碼寫你的 UI,或者使用 storyboard 拖拖拽拽就 ok
- 在 didReceiveNotification 方法里接收推送內(nèi)容,然后各種處理邏輯、傳值、展示 UI 等等。當點擊了 actions,也會走到這里,并且包含一個 action 的字段,判斷點擊了哪個 action 進而相應(yīng)的更新你的 UI。
Info.plist
-
需要在這里讓系統(tǒng)知道,哪個 id 字段會觸發(fā)你這個 extension。
高亮部分字段的值,需要跟 Notification Actions 的 category id 值一樣,這樣收到推送時,就會同時觸發(fā) Notification content + Notification actions。
-
同時這里也可以添加多個值,用于收到不同的推送,展示類似的 UI。
比如接受聚會邀請和提醒聚會邀請,UI 相近,操作卻不同。
調(diào)試
當你各種 UI 展示后,會發(fā)現(xiàn)存在 2 個問題。
其一
是系統(tǒng)會自動展示一遍收到的推送內(nèi)容,這部分很可能跟你的內(nèi)容是重復的。
解決方法
在 Info.plist 中添加如下字段,并且把值設(shè)為 YES 即可隱藏系統(tǒng)默認的展示。
其二
是展示內(nèi)容比較少的時候,系統(tǒng)仍然會以最大的界面展示出來,會露出很多空白部分。
解決方法
方法一:在 viewDidLoad 中調(diào)整 self 的 size 以達到一個合適的尺寸。如下獲取了 size,并修改至一半的高度。
- (void)viewDidLoad {
[super viewDidLoad];
CGSize size = self.view.bounds.size;
self.preferredContentSize = CGSizeMake(size.width, size.height/2);
}
效果如下所示,仔細看你會發(fā)現(xiàn)存在小 bug,先展示了完整的高度,然后瞬間變成一半的高度,看起來有個高度適應(yīng)的動畫的樣子。導致這種結(jié)果的原因是系統(tǒng)準備展示推送的時候,還沒有執(zhí)行到你的代碼(展示從系統(tǒng)層級到 App 層級的過程),這是蘋果內(nèi)部的機制所致。
方法二:還是在 Info.plist 文件添加新的字段,設(shè)置縮放比例。
這樣系統(tǒng)層級會預先讀取該數(shù)據(jù),用于展示。當然有時候展示的內(nèi)容不同,需要的高度不同,而這里只能設(shè)置成唯一的固定值。不過這也是現(xiàn)階段蘋果所能給你提供的可行方法了。
然后最終的展示效果如下,沒有上面那個不舒服的高度調(diào)整動畫了。
小結(jié)
感覺 Notification Content 的功能極其強大,有了它之后連 App 都不需要再啟動了的樣子(只要能合理的設(shè)計展示內(nèi)容和操作),省去了用戶每次為了一項簡單操作都要進行「啟動 App - 操作 - 切換到多任務(wù)界面 - 退出 App」這樣的繁瑣過程。原本用戶看到推送可能不太有意愿去查看詳細內(nèi)容,現(xiàn)在他只需要很簡單的操作就能快速的查看,推送的點開率應(yīng)該會因此而大幅增加吧。
究其如此便捷的原因,Notification Service Extension 和 Notification Content 都是獨立于項目的 target,收到推送后,系統(tǒng)會單獨運行這兩個 target,完全不會在此時去啟動 App 并執(zhí)行 App 中大量的代碼,童鞋們在調(diào)試的時候也可以注意這一點。
以上
《玩轉(zhuǎn) iOS 10 推送》就全部結(jié)束了,更多分享內(nèi)容將會在之后奉上。