iOS10通知(四)--带事件通知的发送和处理
2017-03-31 16:23
405 查看
所谓的带事件通知只是我的一个理解,原意是可交互的通知
1、在NotificationHandle类中增加交互通知的实现方法
5、远程推送的payload内容
{
"aps":{
"alert":{
"title":"事件通知",
"body":"这是一个带事件的通知"
},
"category":"conment" //标识这是一个交互通知,用于代理事件中获取后处理
}
}
6、收到消息后,通知上使用3D Touch即可看到效果,效果图如下
1、在NotificationHandle类中增加交互通知的实现方法
//注册通知中的action事件 -(void)registerNotificationCategory { //带评论的通知事件注册 UNTextInputNotificationAction *inputAction = [UNTextInputNotificationAction actionWithIdentifier:@"input" title:@"评论" options:UNNotificationActionOptionForeground textInputButtonTitle:@"发送" textInputPlaceholder:@"请输入评论内容.."]; UNNotificationAction *openAction = [UNNotificationAction actionWithIdentifier:@"open" title:@"打开" options:UNNotificationActionOptionForeground]; UNNotificationAction *cancleAction = [UNNotificationAction actionWithIdentifier:@"cancle" title:@"取消" options:UNNotificationActionOptionDestructive]; UNNotificationCategory *tapCategory = [UNNotificationCategory categoryWithIdentifier:@"comment" actions:@[inputAction,openAction,cancleAction] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction]; //自定义UI通知的时间注册 UNNotificationAction *nextAction = [UNNotificationAction actionWithIdentifier:@"switch" title:@"切换" options:UNNotificationActionOptionAuthenticationRequired]; UNNotificationCategory *customCategory = [UNNotificationCategory categoryWithIdentifier:@"customUI" actions:@[nextAction,openAction,cancleAction] intentIdentifiers:@[] options:UNNotificationCategoryOptionNone]; // NSSet *set = [[NSSet alloc]initWithObjects:tapCategory, customCategory, nil]; [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:set]; }2、修改代理事件中的方法,增加交互处理,红色标注的为新增
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{ //收到推送的请求 UNNotificationRequest *request = response.notification.request; //收到的内容 UNNotificationContent *content = request.content; //收到用户的基本信息 NSDictionary *userInfo = content.userInfo; //收到消息的角标 NSNumber *badge = content.badge; //收到消息的body NSString *body = content.body; //收到消息的声音 UNNotificationSound *sound = content.sound; //推送消息的副标题 NSString *subtitle = content.subtitle; //推送消息的标题 NSString *title = content.title; if ([response.notification.request.trigger isKindOfClass:[UNNotificationTrigger class]]) { NSLog(@"点击了通知:%@\n",userInfo); } else{ NSLog(@"通知:{\nbody:%@,\ntitle:%@,\nsubtitle:%@,\nbadge:%@,\nsound:%@,\nuserInfo:%@}",body,title,subtitle,badge,sound,userInfo); } //处理消息的事件 NSString *category = content.categoryIdentifier; if ([category isEqualToString:@"comment"]) { [self handCommnet:response]; } completionHandler(); } -(void)handCommnet:(UNNotificationResponse *)response { NSString *actionType = response.actionIdentifier; NSString *textStr = @""; if ([actionType isEqualToString:@"input"]) { UNTextInputNotificationResponse *temp = (UNTextInputNotificationResponse *)response; textStr = temp.userText; } else if ([actionType isEqualToString:@"open"]) { textStr = @"open"; } else if ([actionType isEqualToString:@"cancle"]) { textStr = @""; } NSLog(@"你刚输入的内容是:%@",textStr); }3、在AppDelegate中注册交互事件,红色标注为新增
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. _window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds]; RootViewController *rootVC = [[RootViewController alloc]init]; UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:rootVC]; _window.rootViewController = nav; [_window makeKeyWindow]; [UNUserNotificationCenter currentNotificationCenter].delegate = [NotificationHandle shareInstance]; [[NotificationHandle shareInstance] authorizationPushNotificaton:application]; [[NotificationHandle shareInstance] registerNotificationCategory]; return YES; }4、发送带交互事件的通知
-(void)btnClicked { UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc]init]; content.body = @"这是一个带交互事件的通知"; content.title = @"交互事件通知";
//此处的唯一标识和NotificationHandle中的一一对应
content.categoryIdentifier = @"comment"; UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:3 repeats:NO]; UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"actionable" content:content trigger:trigger]; [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { // }]; }
5、远程推送的payload内容
{
"aps":{
"alert":{
"title":"事件通知",
"body":"这是一个带事件的通知"
},
"category":"conment" //标识这是一个交互通知,用于代理事件中获取后处理
}
}
6、收到消息后,通知上使用3D Touch即可看到效果,效果图如下
相关文章推荐
- Java事件处理与发送
- 对Exchange 事件ID 9154 DSACCESS 返回 DS 通知出现的错误“0x80004005”的处理
- 关于发送带事件邮件出现开始结束时间相同的处理
- iOS 原生模块 给 Javascript(ReactNative) 发送事件 (通知监听)
- Java事件处理与发送原理
- Qt之事件过滤器 截获消息通知 自定义消息处理事件解析.
- 服务器实时通知客户端方案,服务器发送/推送事件方案(2)server event,典型例子,可以用作股票、新闻信息推送
- iOS10通知(二)--发送通知
- 基础篇:事件的发送和处理
- WSAEventSelectI/O模型中FD_WRITE事件的处理以及如何发送数据
- windows的开发流程--事件通知和消息处理
- Flash as3.0 事件的发送和处理
- 从通知栏进入应用中的某一页面,回退事件的处理
- 利用异常进行系统中通用的消息通知和事件处理
- 第3.1.6节 事件通知与处理
- iOS Notification(二):处理&监听通知事件
- Prototype功能预览七:一个注解分离日志及事件的发送于处理逻辑
- 【网络编程】处理定时事件(二)---利用信号通知
- 微信第三方开发:处理消息和事件页面发送文字信息