iOS8新特性之交互式通知
目前分为四个推送:用户推送,本地推送,远程推送,地理位置推送。
- if (IS_IOS8) {
- //1.创建消息上面要添加的动作(按钮的形式显示出来)
- UIMutableUserNotificationAction *action = [[UIMutableUserNotificationAction alloc] init];
- action.identifier = @"action";//按钮的标示
- action.title=@"Accept";//按钮的标题
- action.activationMode = UIUserNotificationActivationModeForeground;//当点击的时候启动程序
- // action.authenticationRequired = YES;
- // action.destructive = YES;
- UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc] init];
- action2.identifier = @"action2";
- action2.title=@"Reject";
- action2.activationMode = UIUserNotificationActivationModeBackground;//当点击的时候不启动程序,在后台处理
- action.authenticationRequired = YES;//需要解锁才能处理,如果action.activationMode = UIUserNotificationActivationModeForeground;则这个属性被忽略;
- action.destructive = YES;
- //2.创建动作(按钮)的类别集合
- UIMutableUserNotificationCategory *categorys = [[UIMutableUserNotificationCategory alloc] init];
- categorys.identifier = @"alert";//这组动作的唯一标示,推送通知的时候也是根据这个来区分
- [categorys setActions:@[action,action2] forContext:(UIUserNotificationActionContextMinimal)];
- //3.创建UIUserNotificationSettings,并设置消息的显示类类型
- UIUserNotificationSettings *notiSettings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIRemoteNotificationTypeSound) categories:[NSSet setWithObjects:categorys, nil nil]];
- [application registerUserNotificationSettings:notiSettings];
- }else{
- [application registerForRemoteNotificationTypes: UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];
- }
一、通过调用 [[UIApplicationsharedApplication]registerForRemoteNotifications];来实现
application:didRegisterForRemoteNotificationsWithDeviceToken:和application:didFailToRegisterForRemoteNotificationsWithError:的回调
二、设置 UIUserNotificationAction和UIMutableUserNotificationCategory
UIUserNotificationAction的设置:
UIMutableUserNotificationAction *cancelAction = [[UIMutableUserNotificationAction alloc] init];
[cancelAction setIdentifier:@"CancelNotificationActionIdentifier"];
[cancelAction setTitle:@"Cancel"];
[cancelAction setActivationMode:UIUserNotificationActivationModeBackground];
[cancelAction setAuthenticationRequired:YES];
[cancelAction setDestructive:YES];
identifier
User notificaton aciton的唯一标示
title
User notificaton aciton button的显示标题
activationMode
UIUserNotificationActivationModeForeground 激活App并打开到前台展示
UIUserNotificationActivationModeBackground 在后台激活App,测试时发现如果没有启动App(App不在后台也没有打开),那么执行这种模式下的操作,App不会打开;如果App已经在前台了,那么执行这种模式下的操作,App依然在前台。
authenticationRequired
如果设置为YES,执行这个操作的时候必须解锁设备,反之,无需解锁。
如果activationMode为UIUserNotificationActivationModeForeground时,authenticationRequired被作为YES处理。
测试时发现,如果用户设备有密码,在锁屏时authenticationRequired设置为YES执行操作时需要用户输入密码,如果设置为NO则不需要用户输入密码,如果用户设备没有密码,那么无论如何设置都不需要输入密码。
destructive
标示操作按钮是否为红色,只有在锁屏和从通知中心向左滑动出现时才有这种突出显示,在顶部消息展示时没有这种突出效果。
UIMutableUserNotificationCategory的设置:
UIMutableUserNotificationCategory *notificationCategory = [[UIMutableUserNotificationCategory alloc] init];
[notificationCategory setIdentifier:@"NotificationCategoryIdentifier"];
[notificationCategory setActions:@[acceptAction, cancelAction]
forContext:UIUserNotificationActionContextDefault];
identifier
category的唯一标示,identifier的值与payload(从服务器推送到客户端的内容)中category值必须一致。
actions
UIUserNotificationAction 数组,如果设置为nil,那么将不会显示操作按钮。
context
UIUserNotificationActionContextDefault 通知操作的默认Context,在这种情况下,你可以指定4个自定义操作。(还未验证)
UIUserNotificationActionContextMinimal 通知操作的最小Context,在这种情况下,你可以指定2个自定义操作。(还未验证)
三、设置 UIUserNotificationSettings
UIUserNotificationType notificationTypes = (UIUserNotificationTypeAlert|
UIUserNotificationTypeSound|
UIUserNotificationTypeBadge);
NSSet *categoriesSet = [NSSet setWithObject:notificationCategory];
UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings settingsForTypes:notificationTypes
categories:categoriesSet];
设置通知所支持的类型和Category,这里没有难点,不过多解释。
四、注册通知 registerUserNotificationSettings
[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
在iOS8中通过以上方式注册通知,可以根据操作系统版本做区分处理
五、处理回调事件
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler
{
if([identifier isEqualToString:@"CancelNotificationActionIdentifier"])
{
NSLog(@"You chose cancel action.");
}
else if ([identifier isEqualToString:@"AcceptNotificationActionIdentifier"])
{
NSLog(@"You chose accept action.");
}
if(completionHandler)
{
completionHandler();
}
}
application
收到通知的对象
identifier
UIUserNotificationAction的唯一标示
userInfo
payload的内容
completionHandler
当执行完指定的操作后,必须在最后调用这个方法
iOS8新特性之交互式通知的更多相关文章
- iOS8新特性(1)——UIAlertController
一.iOS8介绍 iOS8 新特性,主要是UI上进行了统一 1.UIAlertController 2.UIPresentaionController:管理所有通过modal出来的控制器(看笔记) 3 ...
- iOS8 新特性
iOS8新特性主要体现在4方面 1.UIAlertController 对alert&actionSheet的封装 UIAlertController.h 提示框按钮的选择 typedef N ...
- ios8新特性widget开发-b
os8发布已经有一段时间了,伴随着ios8同时也出现了许多新的特性,ios系统将会越来越开放,这是好事.其中一个新特性就是在下拉通知栏里加入了个性的widget,开发者可以自己定义widget的样式内 ...
- iOS8新特性
1. App Extension Programming Guide 2.LocalAuthentication.framework - Touch ID Authentication 3.Local ...
- iOS8新特性之基于地理位置的消息通知UILocalNotification
苹果在WWDC2014上正式公布了全新的iOS8操作系统. 界面上iOS8与iOS7相比变化不大,只是在功能方面进行了完好. ...
- iOS8新特性(1)-UIPopoverPresentationController使用
从iOS 8开始,苹果提出新的 UIPopoverPresentationController代替UIPopoverController: 新的UIPopoverPresentationControl ...
- 利用iOS8新特性计算cell的实际高度
在计算cell的实际高度是 我们一般是通过计算frame 拿到最底部一个控件的最大Y值从而的到cell 的高度 算来算去 比较麻烦 其实,iOS8已经提供了直接通过Cell高度自适应的方法了,根 ...
- iOS iOS8新特性--UIPopoverPresentationController
1.回顾UIPopoverController的使用,下面这份代码只能在ipad下运行 // 初始化控制器,SecondViewController类继承自UIViewController Secon ...
- Ios8新特性-应用程序扩展
一.什么是应用程序扩展? 应用程序扩展不是一个应用,它是主体应用程序(containing app)中一个单独的包,并能生成单独的二进制文件供其他应用调用. 个人感觉,类似于WP中的启动器,把系统当个 ...
随机推荐
- NFA
任意正则表达式都存在一个与之对应的NFA,反之亦然. 正则表达式 ((A*B|AC)D)对应的NFA(有向图), 其中红线对应的为该状态的ε转换, 黑线表示匹配转换 我们定义的NFA具有以下特点: 正 ...
- TLD算法
TLD算法好牛逼一个,这里有个视频,是作者展示算法的效果,http://www.56.com/u83/v_NTk3Mzc1NTI.html.下面这个csdn博客里有人做的相关总结,感觉挺好的,收藏了! ...
- JavaWeb详细学习路线图
- Java攻城狮学习路线 - 图转自网络.
- GCC G++ Make CMake自我科普
Linux下gcc g++ make cmake 联系和区别 C/C++程序从编写到可执行一般经历这几个阶段 编写源代码 编译器编译代码生成目标文件,如.o文件 链接器链接目标文件和其他目标文件/库文 ...
- 百度map API
1.做demo用的 http://developer.baidu.com/map/jsdemo.htm demo代码(外部使用的话需要提供密钥): <!DOCTYPE html> < ...
- 【Oracle】手工建库启动到nomount状态时错误ORA-09925,ORA-01017
配置好pfile和口令文件后启动数据库到nomount状态下出现错误: [oracle@localhost ~]$ sqlplus / as sysdba SQL*Plus: Release 11.2 ...
- java控制台输入输出字符串
一.实例说明 本实例通过输入流(System.in)实现从控制台接受用户输入信息,并将该信息输出到控制台. 运行效果如下图: 二.实现代码 三.要点说明 该实例的关键就是用到了System类的输入流, ...
- switch 语句来选择要执行的多个代码块之一。
switch(n) { case 1: 执行代码块 1 break; case 2: 执行代码块 2 break; default: n 与 case 1 和 case 2 不同时执行的代码 }
- DB2常用运维命令
DB2是IBM公司推出关系型数据库管理系统.主要应用于银行.医院等大型机构.现今DB2主要包含以下三个系列:DB2 for Linux, UNIX and Windows(LUW) . DB2在Lin ...
- Camera三维动画
一.概述 在Android中说到3D开发,我们首先想到的是OpenGL,但用起来比较复杂繁琐,不适合做应用级别的3D变换.Android为我们提供了一个简化版的3D开发入口:Camera(这里的Cam ...