上一篇 咱們聊了一些:
- iOS 10 before 推送的流程
- iOS 10 beta 推送的基本使用方法
- 以及跟 iOS 10 before 推送的區別
這一篇咱們將繼續探討 iOS 10 推送,并向大家介紹一些進階的內容。
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];
-
創建
category
可添加多個
action
的數組,就像圖片中一樣,有多種操作其中的
id
,需要填寫你想要添加到哪個推送消息的id
-
創建:
UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"message" actions:@[action] minimalActions:@[action] intentIdentifiers:@[] options:UNNotificationCategoryOptionNone];
-
把
category
添加到通知中心:
[[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithArray:@[category]]];
4. 觸發方式:
- Remote Notifications 配置 payload,指定其中 category 的值與第 2 步中 Identifier 一致:
```objc
{
aps : {
alert : "Welcome to WWDC !",
category : "message"
}
}
```
- Local Notifications 只需要在創建 contnet 的時候指定 Id 即可:(content 相關內容請參照 [上一篇](http://www.lxweimin.com/p/2f3202b5e758) 中的 Content 部分)
```objc
content。categoryIdentifier = @"message";
```
?
#### Dismiss Actions
鎖屏及在通知中心收到推送,側滑,會展示 action。
只要點擊 Clear 就可以將該條推送清除,并且重復的內容不會被發送到你的其他 iOS 設備上。


跟 Notification Actions 只有一點小區別,就是添加 action 到 category 的時候,增加一個 option 的值 UNNotificationCategoryOptionCustomDismissAction:
```objc
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);
}