目前分为四个推送:用户推送,本地推送,远程推送,地理位置推送。

  1. if (IS_IOS8) {
  2. //1.创建消息上面要添加的动作(按钮的形式显示出来)
  3. UIMutableUserNotificationAction *action = [[UIMutableUserNotificationAction alloc] init];
  4. action.identifier = @"action";//按钮的标示
  5. action.title=@"Accept";//按钮的标题
  6. action.activationMode = UIUserNotificationActivationModeForeground;//当点击的时候启动程序
  7. //    action.authenticationRequired = YES;
  8. //    action.destructive = YES;
  9. UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc] init];
  10. action2.identifier = @"action2";
  11. action2.title=@"Reject";
  12. action2.activationMode = UIUserNotificationActivationModeBackground;//当点击的时候不启动程序,在后台处理
  13. action.authenticationRequired = YES;//需要解锁才能处理,如果action.activationMode = UIUserNotificationActivationModeForeground;则这个属性被忽略;
  14. action.destructive = YES;
  15. //2.创建动作(按钮)的类别集合
  16. UIMutableUserNotificationCategory *categorys = [[UIMutableUserNotificationCategory alloc] init];
  17. categorys.identifier = @"alert";//这组动作的唯一标示,推送通知的时候也是根据这个来区分
  18. [categorys setActions:@[action,action2] forContext:(UIUserNotificationActionContextMinimal)];
  19. //3.创建UIUserNotificationSettings,并设置消息的显示类类型
  20. UIUserNotificationSettings *notiSettings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIRemoteNotificationTypeSound) categories:[NSSet setWithObjects:categorys, nil nil]];
  21. [application registerUserNotificationSettings:notiSettings];
  22. }else{
  23. [application registerForRemoteNotificationTypes: UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];
  24. }

一、通过调用 [[UIApplicationsharedApplication]registerForRemoteNotifications];来实现

application:didRegisterForRemoteNotificationsWithDeviceToken:application:didFailToRegisterForRemoteNotificationsWithError:的回调

二、设置 UIUserNotificationActionUIMutableUserNotificationCategory

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

当执行完指定的操作后,必须在最后调用这个方法

转载于:https://www.cnblogs.com/ritian/p/5436334.html

iOS8新特性之交互式通知相关推荐

  1. (0058)iOS开发之iOS8新特性IBDesignable的使用

    在OC和Swift中使用IBDesignable/IBInspectable iOS8新特性IBDesignable/IBInspectable,可以直接在XIB或者Storyboard中直接,设置U ...

  2. ios8新特性widget开发(手机上和通知栏并列的今天的制作)

    ios8发布已经有一段时间了,伴随着ios8同时也出现了许多新的特性,ios系统将会越来越开放,这是好事.其中一个新特性就是在下拉通知栏里加入了个性的widget,开发者可以自己定义widget的样式 ...

  3. iOS8新特性 计算 cell 的高度

    http://tutuge.me/2015/08/08/autolayout-example-with-masonry2/ 1.tableview: 自动计算 tableVIew 的 cell 的高度 ...

  4. iOS8最新谍照曝光新特性PP帮手抢先看

    跟着本年WWDC苹果开发者大会时刻的断定,关于iOS8何时发布也有了新的发展,iOS8很有能够在6月2日的WWDC大会上正式问世!在iOS8到来之前,全部猜测和曝光都是有能够变成实际的,PP帮手取得一 ...

  5. iOS6、7、8、9新特性汇总和适配说明

    一.关于内存警告ios6中废除了viewDidUnload,viewWillUnload这两个系统回调,收到内存警告时在didReceiveMemoryWarning中进行相关的处理. 二.关于屏幕旋 ...

  6. iOS 6 7 8 9新特性

    iOS6.7.8.9新特性总汇和适配说明 时间:2015-11-13 10:13:37      阅读:304      评论:0      收藏:0      [点我收藏+] 标签: 转自:http ...

  7. iOS6、7、8、9区别和新特性

    一.关于内存警告 ios6中废除了viewDidUnload,viewWillUnload这两个系统回调,收到内存警告时在didReceiveMemoryWarning中进行相关的处理. 1 2 3 ...

  8. WWDC 2018:iOS 12 通知的新特性

    Session 710 : What's New in User Notifications iOS 10 新增的 UserNotifications.framework 用一套易用的接口替换了之前版 ...

  9. android 通知历史,Android P新特性:追踪应用通知历史

    原标题:Android P新特性:追踪应用通知历史 IT之家3月9日消息 不久前,谷歌已经正式推出了首个Android P开发者预览版,包含了许多新特性.对此,IT之家也进行了一系列报道.该系统的新特 ...

最新文章

  1. Iterator:访问数据集合的统一接口
  2. HP 3055 恢复出厂
  3. 深度学习之循环神经网络(12)预训练的词向量
  4. 创建设计模式 - 原型设计模式
  5. 深入了解preventDefault与stopPropagation
  6. Linux系统修改编码(转)
  7. 音乐网站搭建源码Sourcecode
  8. 打开计算机没显示硬盘分区,电脑新插了一个硬盘没显示怎么办
  9. SpringBoot更换Apache Log4 2.15.0-rc2j漏洞补丁
  10. python 正则匹配 条件太多怎么办_python正则中re.findall匹配多个条件
  11. 搭建网站从服务器购买备案到域名注册整个流程
  12. WebStorm如何设置字体大小
  13. 移动平均线rolling()与加权移动平均线ewm()
  14. # DZY Love Math 系列
  15. 加装固态,重装系统(双系统)
  16. vue + echarts 之世界地图
  17. 计算机输入法在桌面显示不出来怎么办,电脑桌面上的输入法切换键没有了怎么办...
  18. php5.3不能连接mssql数据库的解决方法
  19. 三星 android 刷机,三星安卓手机如何刷机 三星安卓手机刷机通用方法介绍【教程】...
  20. Centos 7安装Squid代理服务及构建传统代理

热门文章

  1. C语言:constkeyword、结构体
  2. adb 命令 (01)
  3. linux运维实战练习及linux shell脚本、awk、sed工具命令学习总结
  4. Reactive Streams
  5. android.os.NetWorkOnMainThreadExcetion
  6. GCC中的内嵌汇编语言
  7. 使用repeater,遍历数据,不规则排序,不同的样式之间切换
  8. python threading condition使用_Python threading模块condition原理及运行流程详解
  9. OpenStack 虚拟机热迁移流程图
  10. Windwos 08R2_DNS全面图文详解