上一篇 我们聊了一些:json
这一篇我们将继续探讨 iOS 10 推送,并向你们介绍一些进阶的内容。数组
在 iOS 10 中,能够容许推送添加交互操做 action
,这些 action
可使得 App 在前台或后台执行一些逻辑代码。而且在锁屏界面经过 3d-touch 触发。如:推出键盘进行快捷回复,该功能以往只在 iMessage 中可行。
(Notification Actions 在 iOS 8 引入,快捷回复在 iOS 9 引入,在 iOS 10 中,这些 API 被统一。)
ui
在 iOS 10 中,这叫 category
,是对推送功能的一个拓展,能够经过 3d-touch 触发。编码
建立 action
加密
即一项交互操做spa
title
是交互按钮的内容3d
options
可让该 action
成为一条可在前台执行的 action
code
建立:orm
UNNotificationAction *action = [UNNotificationAction actionWithIdentifier:@"reply" title:@"Reply" options:UNNotificationActionOptionNone];复制代码
建立 category
cdn
可添加多个 action
的数组,就像图片中同样,有多种操做
其中的 id
,须要填写你想要添加到哪一个推送消息的 id
建立:
UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"message" actions:@[action] minimalActions:@[action] intentIdentifiers:@[] options:UNNotificationCategoryOptionNone];复制代码
把 category
添加到通知中心:
[[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithArray:@[category]]];复制代码
触发方式:
Remote Notifications 配置 payload,指定其中 category 的值与第 2 步中 Identifier 一致:
{
aps : {
alert : "Welcome to WWDC !",
category : "message"
}
}复制代码
Local Notifications 只须要在建立 contnet 的时候指定 Id 便可:(content 相关内容请参照 上一篇 中的 Content 部分)
content。categoryIdentifier = @"message";复制代码
锁屏及在通知中心收到推送,侧滑,会展现 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 的值复制代码
用户点击这些 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();
}复制代码
能够在手机「接收到推送以后、展现推送以前」对推送进行处理,更改、替换原有的内容。
使用了这个玩意,大家公司原有发送推送的 payload 能够彻底不变,而在客户端对接收到的内容(只有一条字符串)进行加工,从而适配 iOS 10 的展现效果(标题+副标题+内容)。
值得大家 App 充分发挥的是能够作如下事情:
不急,咱们先来介绍怎么
先在 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);
}复制代码
{
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);
}复制代码