iOS10通知(四)--带事件通知的发送和处理
来源:互联网 发布:nba01年总决赛数据 编辑:程序博客网 时间:2024/06/05 04:30
所谓的带事件通知只是我的一个理解,原意是可交互的通知
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即可看到效果,效果图如下
0 0
- iOS10通知(四)--带事件通知的发送和处理
- iOS10通知(二)--发送通知
- iOS10通知(三)--通知的取消和修改
- Qt中的消息通知和事件发送
- android 发送通知栏通知的灵异事件
- iOS10本地通知的探索
- ios10下的通知更新
- 模仿iOS10的下拉通知
- Poco 的通知和事件
- windows的开发流程--事件通知和消息处理
- 程序发送和取消状态栏的通知
- PendingIntent的通知发送
- 事件和通知
- iOS10--消息通知的基本使用
- iOS10里的通知与推送
- iOS10里的通知与推送
- iOS10--消息通知的基本使用
- iOS10--消息通知的基本使用
- 数据压缩实验二:BMP2YUV文件转换
- 详谈 JavaScript 数组去重 2 :性能优化篇
- oracle----buffer、图形相交、使用body中的函数、确认表存在,导入文本入库
- 127.0.0.1和0.0.0.0地址的区别
- @ResponseBody
- iOS10通知(四)--带事件通知的发送和处理
- Codeforces 702D Road to Post Office【捎带数学的思维题】
- 升级Xcode后,出现大批警告
- 选择排序
- java Type 详解
- Shell字符串
- 设计模式之责任链模式
- 查询哪些存储过程是每次重编译的
- [推荐算法]UserCF,基于用户的协同过滤算法