iOS 10 came
在今年 6月14號 蘋果開發者大會 WWDC 2016 之後,筆者趕緊就去 apple 的開發者網站下載了最新的 Xcode 8 beta 和 iOS 10 beta,然後在自己的手機上裝了 iOS 10 beta ,狠狠地體驗了一把。
可以說 iOS 10 無論從界面風格,還是 Framework 都做了很多改動。最直觀的感受就是界面的圓角增多了,系統動畫更加多樣和流暢,系統 App 的功能也變得更豐富了。
而 iOS 10 裏的推送功能,也較之前更加強大,
今天我們就來聊聊 iOS 10 裏的推送功能。
Notifications before iOS 10
首先我們一起簡單回顧下 iOS 10 以前的推送服務。
iOS 推送分爲 Local Notifications(本地推送) 和 Remote Notifications(遠程推送),先看 2 張圖:
Local Notifications
Remote Notifications
簡單的說就是本地推送通過 App 本地定製,加入到系統的 Schedule 裏,然後在指定的時間推送指定文字。而遠程推送通過服務端向蘋果推送服務器 Apple Push Notification Service (APNs) 發送 Notification Payload,之後 APNs 再將推送下發到指定設備的 指定 App 上。
以及 iOS 7 之後在不顯式地彈窗打擾用戶的情況下,進行的靜默推送:
Silent Push
具體做法可以參考 iOS 7 Background Remote Notification
User Notifications Framework
好,扯了這麼多,該進入今天的正題了 —— User Notifications Framework 。
首先在 AppDelegate.m
中
import
#import <UserNotifications/UserNotifications.h>
註冊推送
以下分別是 iOS 10 之前和之後的註冊方式,其中的 UNAuthorizationOptions
裏還可以找到 1 個 UNAuthorizationOptionCarPlay
的值是專爲車載系統定製的值。
- (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
之前註冊推送服務,ios 8 及之前使用了不同的 API,並且返回結果也不同。現在 apple 不僅統一了這個 API,而且我們可以獲取到用戶更加詳細的設定了。
[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
以前只能展示一條文字,現在可以有 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) { }];
推送小結
然後整個推送的過程就變成了醬紫:
Local Notifications 通過定義
Content
和Trigger
向UNUserNotificationCenter
進行request
這三部曲來實現。Remote Notifications 則向
APNs
發送Notification Payload
。
Notification Handling
設定了推送,然後就結束了?iOS 10 並沒有這麼簡單!
通過實現協議,使 App 處於前臺時捕捉並處理即將觸發的推送:
@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
中還有諸如刪除所有推送、查看已經發出的推送、刪除已經發出的推送等等強大的接口。
刷新原有的推送後,在通知中心的顯示裏,也會有相應的變化,這裏注意第 2 條信息,現在比分是 1:0
比分刷新後爲 1:1,在不產生新的推送條目的情況下位置被前置了!
試想利用這個方法,不斷的刷新推送,是不是就可以做到讓自己 App 的推送內容始終展示在用戶手機通知中心的最頂端,力壓其餘所有內容了呢?總感覺有點不厚道啊~
Advanced Notifications
關於推送的更多類似 Media Attachments
的高級功能,我們將在下一篇裏詳細討論。
Media Attachments
爲推送添加更多媒體附件,諸如圖片、音樂
Notification Actions
在 iOS 10 中,可以允許推送添加交互操作 action
,這些 action
可以使得 App 在前臺或後臺執行一些邏輯代碼。並且在鎖屏界面通過 3d-touch 觸發。如:推出鍵盤進行快捷回覆,該功能以往只在 iMessage 中可行。
(Notification Actions 在 iOS 8 引入,快捷回覆在 iOS 9 引入,在 iOS 10 中,這些 API 被統一。)
在 iOS 10 中,這叫 category
,是對推送功能的一個拓展,可以通過 3d-touch 觸發。
創建
action
即一項交互操作
title
是交互按鈕的內容options
可以讓該action
成爲一條可在前臺執行的action
創建:
UNNotificationAction *action = [UNNotificationAction actionWithIdentifier:@"reply" title:@"Reply" options:UNNotificationActionOptionNone];
可添加多個
action
的數組,就像圖片中一樣,有多種操作其中的
id
,需要填寫你想要添加到哪個推送消息的id
創建:
UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"message" actions:@[action] minimalActions:@[action] intentIdentifiers:@[] options:UNNotificationCategoryOptionNone];
Remote Notifications 配置 payload,指定其中 category 的值與第 2 步中 Identifier 一致:
{ aps : { alert : "Welcome to WWDC !", category : "message" } }
Local Notifications 只需要在創建 contnet 的時候指定 Id 即可:(content 相關內容請參照 上一篇 中的 Content 部分)
content。categoryIdentifier = @"message";
Dismiss Actions
鎖屏及在通知中心收到推送,側滑,會展示 action。
只要點擊 Clear 就可以將該條推送清除,並且重複的內容不會被髮送到你的其他 iOS 設備上。
跟 Notification Actions 只有一點小區別,就是添加 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、觸發鍵盤、清除通知或是有其他的響應,這些全部只需要實現協議 UNUserNotificationCenterDelegate 中的一個方法就可以控制:
@interface ClassName () <UNUserNotificationCenterDelegate> -(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{ }
其中的 response 包含以下內容:
其中的 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 //假設點擊了輸入內容的 UNTextInputNotificationAction 把 response 強轉類型 UNTextInputNotificationResponse *textResponse = (UNTextInputNotificationResponse*)response; //獲取輸入內容 NSString *userText = textResponse.userText; //發送 userText 給需要接收的方法 [ClassName handleUserText: userText]; }else{ } } completionHandler(); }
Service Extension
可以在手機「接收到推送之後、展示推送之前」對推送進行處理,更改、替換原有的內容。
使用了這個玩意,你們公司原有發送推送的 payload 可以完全不變,而在客戶端對接收到的內容(只有一條字符串)進行加工,從而適配 iOS 10 的展示效果(標題+副標題+內容)。
「接收到推送之後、展示推送之前」:
此時,你獲得了一小段在後臺運行代碼的時間(也可以用來幹別的壞事>。<,可以偷偷的斷點下載你們 App 的更新包)
而如果你更改推送內容出了錯誤,或者你調用什麼方法失敗了,那麼最終會正常的展示最初接收到的推送內容。
Potential uses
值得你們 App 充分發揮的是可以做以下事情:
端到端加密
給推送展示內容添加附件(比如照片、背景音樂),使得內容更加豐富,就像從推送里拉出了一個網頁有木有!
不急,我們先來介紹怎麼
添加 Service Extension
先在 Xcode 打開你的 App 工程,File - New - Target 然後添加這個:
然後會自動創建一個 UNNotificationServiceExtension 的子類 NotificationService,通過完善這個子類,來實現你的需求。
點開 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 讓你可以在後臺處理接收到的推送,傳遞最終的內容給 contentHandler
serviceExtensionTimeWillExpire 在你獲得的一小段運行代碼的時間即將結束的時候,如果仍然沒有成功的傳入內容,會走到這個方法,可以在這裏傳肯定不會出錯的內容,或者他會默認傳遞原始的推送內容
Example payload
{ aps : { alert : "New Message", mutable-content : 1 }, encrypted-content : "#myencryptedcontent"}
首先需要添加 mutable-content : 1,這意味着此條推送可以被 Service Extension 進行更改
同時可以附加一條 encrypted-content,可以提取該內容進行替換
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler { //用你的重編碼方法對該內容進行更改 NSString *decryptedBody = [DecryptClass decrypt: request.content.userInfo[@"encrypted-content"]]; //創建新的 content 並添加修改過的 body UNMutableNotificationContent *newContent = [UNMutableNotificationContent new]; newContent.body = decryptedBody; //回調新的 content contentHandler(newContent); }
Notifications User Interface
我們先來看一下 iOS 10 默認的推送 UI。
包括「橫幅、鎖屏、通知中心 」三處,看起來差不多的樣子。
Media Attachments
推送內容中增圖片、gif、audio、video。
在以上的三個界面都可以通過 3d-touch 觸發。
先一起來看看效果。
添加方法
打開 iOS Xcode Project - File - New - Target - iOS - Notification Service Extension - Next - Product Name 填寫
yourPushNotificationService
- Finish
具體圖示方法,在《中》裏有詳細的介紹。添加文件。把你們定製的各種 media 文件拖拽至上一步系統自動生成的
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.把推送內容轉爲可變類型 self.bestAttemptContent = [request.content mutableCopy]; // 2.獲取 1 中自定義的字段 value NSString *urlStr = [request.content.userInfo valueForKey:@"your-p_w_upload"]; // 3.將文件夾名和後綴分割 NSArray *urls = [urlStr componentsSeparatedByString:@"."]; // 4.獲取該文件在本地存儲的 url NSURL *urlNative = [[NSBundle mainBundle] URLForResource:urls[0] withExtension:urls[1]]; // 5.依據 url 創建 p_w_upload UNNotificationAttachment *p_w_upload = [UNNotificationAttachment p_w_uploadWithIdentifier:urlStr URL:urlNative options:nil error:nil]; // 6.賦值 @[p_w_upload] 給可變內容 self.bestAttemptContent.p_w_uploads = @[p_w_upload]; // 7.處理該內容 self.contentHandler(self.bestAttemptContent); }
先運行你的項目 target 使之在手機上安裝,再運行 Notification Service 的 target,並選擇在你的項目上運行該 Extension。此時可進行 Notification Service 代碼的調試,即在
NotificationService.m
中打斷點可以調試,但是在你的項目中的斷點無法調試。發送 payload 需依照下述格式:
{ aps : { alert : {...}, mutable-content : 1 //必須 } your-p_w_upload : aPicture.png //必須 }
其中:
mutable-content : 1
說明該推送在接收後可被修改,這個字段決定了系統是否會調用 Notification Service 中的方法。your-p_w_upload:是自定義的字段,key 可以自定義(你自己要記住),value 需要是一個完整的文件名(或 url,後續會詳細解釋),即你想要展示的文件。
手機接收後,在任一個能看到推送條目的界面對推送條目進行 3d-touch 強按都可以觸發。(需要 iPhone 6s 及以後設備 & iOS 10)
提示:各種 media 文件大小有一定限制,圖片、視頻等過大都不會被展示,Apple 的意思是:對於圖片,最大寬度也就和屏幕等寬,過大的圖片沒有意義;對於音頻、視頻等,完全可以提供一個短時間預覽部分,更多的內容還是需要用戶點擊推送進入 App 之後對完整的內容進行查看。希望開發者遵從這樣的邏輯進行開發。
Notification Content
iOS 10 新增的另一項 Extension,用於完全自定義推送展示的 UI 界面,響應 Actions 的同時刷新該 UI。簡單的說就是你可以把需要推送的內容(比如一條完整的新聞快訊,包括多條文字+圖片的組合)全部放到一條推送裏,用戶點擊了一個 Action(如贊、踩、關注、甚至評論等),在推送裏立刻刷新 UI(如展示加星動畫、評論內容等)。
特點
需要添加 Notification content extension
完全自定義 UI
推送 UI 不能響應觸摸、點擊、滑動等任何手勢
可以響應 notification actions
下圖中日程表的 UI 完全由開發者自定義,並且在點擊了 Accept 之後,UI 立即發生了變化:
添加方法
打開 iOS Xcode Project - File - New - Target - iOS - Notification Content - Next - Product Name 填寫 yourPushNotificationContent
- Finish
系統會在 Xcode 工程目錄中 自動生成 yourPushNotificationContent
文件夾,並且包含四個文件:NotificationViewController.h
、NotificationViewController.m
、MainInterface.storyboard
、Info.plist
。
NotificationViewController 繼承自 UIViewController,並實現了 UNNotificationContentExtension 協議。
MainInterface.storyboard
拖拖拽拽一個 UI 就出來了 ^。^
NotificationViewController.h/m
你可以在 viewDidLoad 裏各種代碼寫你的 UI,或者使用 storyboard 拖拖拽拽就 ok
在 didReceiveNotification 方法裏接收推送內容,然後各種處理邏輯、傳值、展示 UI 等等。當點擊了 actions,也會走到這裏,並且包含一個 action 的字段,判斷點擊了哪個 action 進而相應的更新你的 UI。
Info.plist
需要在這裏讓系統知道,哪個 id 字段會觸發你這個 extension。
高亮部分字段的值,需要跟 Notification Actions 的 category id 值一樣,這樣收到推送時,就會同時觸發 Notification content + Notification actions。同時這裏也可以添加多個值,用於收到不同的推送,展示類似的 UI。
比如接受聚會邀請和提醒聚會邀請,UI 相近,操作卻不同。
調試
當你各種 UI 展示後,會發現存在 2 個問題。
其一
是系統會自動展示一遍收到的推送內容,這部分很可能跟你的內容是重複的。
解決方法
在 Info.plist 中添加如下字段,並且把值設爲 YES 即可隱藏系統默認的展示。
其二
是展示內容比較少的時候,系統仍然會以最大的界面展示出來,會露出很多空白部分。
解決方法
方法一:在 viewDidLoad 中調整 self 的 size 以達到一個合適的尺寸。如下獲取了 size,並修改至一半的高度。
- (void)viewDidLoad { [super viewDidLoad]; CGSize size = self.view.bounds.size; self.preferredContentSize = CGSizeMake(size.width, size.height/2); }
效果如下所示,仔細看你會發現存在小 bug,先展示了完整的高度,然後瞬間變成一半的高度,看起來有個高度適應的動畫的樣子。導致這種結果的原因是系統準備展示推送的時候,還沒有執行到你的代碼(展示從系統層級到 App 層級的過程),這是蘋果內部的機制所致。
方法二:還是在 Info.plist 文件添加新的字段,設置縮放比例。
這樣系統層級會預先讀取該數據,用於展示。當然有時候展示的內容不同,需要的高度不同,而這裏只能設置成唯一的固定值。不過這也是現階段蘋果所能給你提供的可行方法了。
然後最終的展示效果如下,沒有上面那個不舒服的高度調整動畫了。
小結
感覺 Notification Content 的功能極其強大,有了它之後連 App 都不需要再啓動了的樣子(只要能合理的設計展示內容和操作),省去了用戶每次爲了一項簡單操作都要進行「啓動 App - 操作 - 切換到多任務界面 - 退出 App」這樣的繁瑣過程。原本用戶看到推送可能不太有意願去查看詳細內容,現在他只需要很簡單的操作就能快速的查看,推送的點開率應該會因此而大幅增加吧。
究其如此便捷的原因,Notification Service Extension 和 Notification Content 都是獨立於項目的 target,收到推送後,系統會單獨運行這兩個 target,完全不會在此時去啓動 App 並執行 App 中大量的代碼,童鞋們在調試的時候也可以注意這一點。
以上
《玩轉 iOS 10 推送》就全部結束了,更多分享內容將會在之後奉上^。^