82. iOS8 iOS9 通知的变化 微信消息快速回复 快捷输入框

iOS 8 UIUserNotificationSettings

支持版本

v1.8.0 版本开始。

  • 本次iOS 8在推送方面最大的变化就是修改了推送的注册接口,在原本的推送type的基础上,增加了一个categories参数,这个参数的目的是用来注册一组和通知关联起来的button的事件。
  • 这个categories由一系列的 UIUserNotificationCategory组成。每个UIUserNotificationCategory对象包含你的app用来响应本地或者远程通知的信息。每一个对象的title作为通知上每一个button的title展示给用户。当用户点击了某一个button,系统将会调用应用内的回调函数application:handleActionWithIdentifier:forRemoteNotification:completionHandler:或者application:handleActionWithIdentifier:forLocalNotification:completionHandler:。

客户端设置

使用UIUserNotificationCategory
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {

 NSMutableSet *categories = [NSMutableSet set];

 UIMutableUserNotificationCategory *category = [[UIMutableUserNotificationCategory alloc] init];

 category.identifier = @"identifier";

 UIMutableUserNotificationAction *action = [[UIMutableUserNotificationAction alloc] init];

 action.identifier = @"test2";

 action.title = @"test";

 action.activationMode = UIUserNotificationActivationModeBackground;

 action.authenticationRequired = YES;

 //YES显示为红色,NO显示为蓝色
 action.destructive = NO;

 NSArray *actions = @[ action ];

 [category setActions:actions forContext:UIUserNotificationActionContextMinimal];

 [categories addObject:category];
}
使用UIUserNotificationType
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
[APService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)                      categories:categories];
}else{
[APService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)                      categories:nil];
}
使用回调函数
// Called when your app has been activated by the user selecting an action from
// a remote notification.
// A nil action identifier indicates the default action.
// You should call the completion handler as soon as you've finished handling
// the action.
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo
  completionHandler:(void (^)())completionHandler {
}

服务端设置

服务端payload格式:aps增加category字段,当该字段与客户端UIMutableUserNotificationCategory的identifier匹配时,触发设定的action和button显示。

payload example:
{"aps":{"alert":"example", "sound":"default", "badge": 1, "category":"identifier"}}

iOS 9 UIUserNotificationActionBehaviorTextInput

支持版本

v1.8.0 版本开始

  1. 本次iOS 9在推送方面最大的变化就是修改了推送Category的类型,在原本的推送categories的基础上,增加了一个text Action类型,这个参数的目的是用来注册通过通知快捷文字输入的事项。
  2. 这个categories由一系列的 UIUserNotificationCategory组成。每个UIUserNotificationCategory对象允许添加一组UIMutableUserNotificationAction类型的参数来增加通知栏上的项目。如今iOS9在原有的UIMutableUserNotificationAction类型增加了Text输入类型(UIUserNotificationActionBehaviorTextInput),通过behavior来设置(只有iOS9才拥有的属性)。
  3. 回调的方法iOS9使用了两个新的回调方法来处理点击按钮的事件:
- (void)application:(UIApplication *)application handleActionWithIdentifier:(nullableNSString *)identifier forLocalNotification:(UILocalNotification *)notification withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void(^)())completionHandler NS_AVAILABLE_IOS(9_0)

- (void)application:(UIApplication *)application handleActionWithIdentifier:(nullableNSString *)identifier forRemoteNotification:(NSDictionary *)userInfo withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void(^)())completionHandler NS_AVAILABLE_IOS(9_0)

说明:

  • 当Action为UIUserNotificationActionBehaviorTextInput时,需要通过responseInfo的UIUserNotificationActionResponseTypedTextKey来获取输入的文字内容,UIUserNotificationTextInputActionButtonTitleKey获取点击的按钮类型.

  • 当Action为UIUserNotificationActionBehaviorDefault时,responseInfo为nil,通过identifier来区分点击按钮分别是什么来做处理.

客户端设置

设置带有快速回复内容的通知

#ifdef __IPHONE_9_0 
 UIMutableUserNotificationAction *replyAction = [[UIMutableUserNotificationAction alloc]init];
 replyAction.title = @"Reply";
 replyAction.identifier = @"comment-reply";
 replyAction.activationMode = UIUserNotificationActivationModeBackground;
 replyAction.behavior = UIUserNotificationActionBehaviorTextInput;

 UIMutableUserNotificationCategory *category = [[UIMutableUserNotificationCategory alloc]init];
 category.identifier = @"reply";
 [category setActions:@[replyAction] forContext:UIUserNotificationActionContextDefault];
#endif

使用回调函数

- (void)application:(UIApplication *)application handleActionWithIdentifier:(nullable NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void(^)())completionHandler NS_AVAILABLE_IOS(9_0) {
 if ([identifier isEqualToString:@"comment-reply"]) {
 NSString *response = responseInfo[UIUserNotificationActionResponseTypedTextKey];
 //对输入的文字作处理
 }
 completionHandler();
 }

服务端设置

服务端payload格式:aps增加category字段,当该字段与客户端UIMutableUserNotificationCategory的identifier匹配时,触发设定的action和button显示。

payload example:
{"aps":{"alert":"example", "sound":"default", "badge": 1, "category":"reply"}}
参考了文章:http://blog.csdn.net/yujianxiang666/article/details/35260135#comments

你可能感兴趣的:(iOS历程,快捷回复,通知栏输入框,iOS9通知栏,微信消息快速回复)