iOS9开发之新增通知行为详解
苹果在iOS8发布时,收到短信时可以直接在通知栏输入文字并回复,非常炫酷,然而这一功能并未真正开放给开发者。
iOS9新增了用户通知行为UIUserNotificationActionBehaviorTextInput,苹果终于将这一炫酷的功能开放给我们。
具体实现方式为:
1. 设置通知行为:
//1.创建可变通知行为
UIMutableUserNotificationAction * ua = [[UIMutableUserNotificationAction alloc] init];
//2.设置通知行为的表现为文本输入
[ua setBehavior:UIUserNotificationActionBehaviorTextInput];
//3.给action给一个标示符
[ua setIdentifier:@"myReply"];
//4.设置行为激活模式为保持后台运行
[ua setActivationMode:UIUserNotificationActivationModeBackground];
这里设置激活模式时,如果在通知栏回复/阅读之后希望跳转回自己的APP,应当原则前台激活模式,如果回复/阅读之后希望保持后台运行才选择这个模式
2. 设置通知策略:
//1.创建一个可变通知策略
UIMutableUserNotificationCategory * cate = [[UIMutableUserNotificationCategory alloc] init];
//2.给category一个标示符
[cate setIdentifier:@"textCategory"];
//3.为这个策略category制定相关的通知行为action
[cate setActions:@[ua] forContext:UIUserNotificationActionContextDefault];
. 注册通知配置(iOS8以后的方式):
//1.通过上面的策略来创建用户通知配置
UIUserNotificationSettings * settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:[NSSet setWithArray:@[cate]]];
//2.注册这个通知配置
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
经过这三部之后完成了一个自定义通知策略的激活,其通知行为为文本框输入并回复。
发送本地通知
在需要发送通知的时候,只需要设置对应的策略和行为,就能以这种方式来弹出通知。以本地通知UILocalNotification为例:
//1.初始化本地通知
UILocalNotification * noti = [[UILocalNotification alloc]init];
//2.设置通知正文
[noti setAlertBody:[NSString stringWithFormat:@"您有新的消息:%@",[NSDate new].description]];
//3.配置对应的策略和行为(必须之前已经注册过了)
[noti setCategory:@"textCategory"];
[noti setAlertAction:@"myReply"];
//4.弹出通知
[[UIApplication sharedApplication] presentLocalNotificationNow:noti];
接收通知行为文本框的回复内容
由于新的通知行为是有输入的,因此跟目前(截止本文撰写日期2015.9月)主流的聊天应用的通知提示方式不同,例如QQ在通知栏设置了ok和取消的按钮,这种方式虽然能够通过ok按钮回复”ok”两个字,但是也只能是这两个字。回复的消息内容是写死(HARD WRITE)的。
而新的通知行为是用户在通知栏输入什么则回复什么,因此APP需要获取到用户输入的内容。
下面2个UIApplicationDelegate的代理方法提供了这种带reply的通知代理:
//本地通知的带回复通知代理
- (void)application:(UIApplication *)application handleActionWithIdentifier:(nullable NSString *)identifier forLocalNotification:(UILocalNotification *)notification withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void(^)())completionHandler NS_AVAILABLE_IOS(9_0);
//远程通知的带回复通知代理
- (void)application:(UIApplication *)application handleActionWithIdentifier:(nullable NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void(^)())completionHandler NS_AVAILABLE_IOS(9_0);
不管具体是本地通知和本地通知,获取到通知栏用户输入的文本内容,只需要去responseInfo这个字典中取UIUserNotificationActionResponseTypedTextKey这个key对应的string就好了。
还是以本地通知为例:
- (void)application:(UIApplication *)application handleActionWithIdentifier:(nullable NSString *)identifier forLocalNotification:(UILocalNotification *)notification withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void(^)())completionHandler {
NSLog(@"用户在文本框中输入的内容:%@",responseInfo[UIUserNotificationActionResponseTypedTextKey]);
completionHandler();
}
iOS9开发之新增通知行为详解的更多相关文章
- 重新想象 Windows 8 Store Apps (35) - 通知: Toast 详解
[源码下载] 重新想象 Windows 8 Store Apps (35) - 通知: Toast 详解 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 通知 Toa ...
- 重新想象 Windows 8 Store Apps (36) - 通知: Tile 详解
[源码下载] 重新想象 Windows 8 Store Apps (36) - 通知: Tile 详解 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 通知 Tile ...
- PHP开发中常见的安全问题详解和解决方法(如Sql注入、CSRF、Xss、CC等
页面导航: 首页 → 网络编程 → PHP编程 → php技巧 → 正文内容 PHP安全 PHP开发中常见的安全问题详解和解决方法(如Sql注入.CSRF.Xss.CC等) 作者: 字体:[增加 减小 ...
- Android游戏开发之旅 View类详解
Android游戏开发之旅 View类详解 自定义 View的常用方法: onFinishInflate() 当View中所有的子控件 均被映射成xml后触发 onMeasure(int, int) ...
- Android开发:文本控件详解——TextView(一)基本属性
一.简单实例: 新建的Android项目初始自带的Hello World!其实就是一个TextView. 在activity_main.xml中可以新建TextView,从左侧组件里拖拽到右侧预览界面 ...
- 微信公众开发URL和token填写详解
微信公众开发URL和token填写详解 方法/步骤 作为一名微信公众号开发者,别人进入你的微信公众号,肯定会看见某些网页,或者给你发某些信息,你需要实时自动回复,所以你需要一个24小时为用户服 ...
- iOS开发--常用技巧 (MJRefresh详解)
iOS开发--常用技巧 (MJRefresh详解) https://github.com/CoderMJLee/MJRefresh 下拉刷新01-默认 self.tableView.head ...
- Android开发:文本控件详解——TextView(二)文字跑马灯效果实现
一.需要使用的属性: 1.android:ellipsize 作用:若文字过长,控制该控件如何显示. 对于同样的文字“Android开发:文本控件详解——TextView(二)文字跑马灯效果实现”,不 ...
- legend3---Windows 7/8/10 系统下Laravel框架的开发环境安装及部署详解(Vagrant + Homestead)
legend3---Windows 7/8/10 系统下Laravel框架的开发环境安装及部署详解(Vagrant + Homestead) 一.总结 一句话总结: 1.安装的话就是下载好git,va ...
随机推荐
- HDU 4628
这是一个大水题啊... 因为比赛时不会算复杂度耽误半天. i从0到2^n枚举集合i的所有分割两半的情况的复杂度为O(3^n),可以想象这个过程相当于是给每个位标记0,1,2(0表示不选,1,2表示两个 ...
- Why Does Everyone Else Appear to Be Succeeding?
Why Does Everyone Else Appear to Be Succeeding? —Steven G. Krantz When you are a student, it will a ...
- Qt 经典出错信息之”Basic XLib functionality test failed!”(Z..z..)
此完整出错信息是在./configure阶段 Basic XLib functionality test failed! You might need to modify the include an ...
- 教你50招提升ASP.NET性能(八):检查你使用了什么客户端脚本
(14)Review what client scripts you are using 招数14: 检查你使用了什么客户端脚本 Out of the box, many ASP.NET projec ...
- TableControl大小变化
TableControl跟随Form大小变化: 选中TableControl,而不是TablePage,右侧Layout: 可以对其设置居上.居下等位置
- git在windows下clone、pull或者push内存溢出的解决办法
发现国内使用git来真正管理源码的人不多,特别是大数据量的源码,今天使用git clone android的源码时突然出现remote out of memery,解决办法: git config - ...
- android实现弧形进度表盘效果
附件:Cirbar.rar
- [GIF] Parenting in GIF Loop Coder
In this lesson, we look at how you can build up complex animations by assigning one shape as the par ...
- [AngularJS] Taking control of your templates using $templateCache
Using $templateCache for quickly retrieval from the cache after first time used. $templateCache main ...
- Starling性能优化技巧十五则
Starling的性能优化要点 一.尽可能减少状态变更 如您所知,Starling使用Stage3D来渲染所有的可见对象.这就意味着所有的绘制都是GPU完成的. 现在,Starling可以一个接一个的 ...