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

  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

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

iOS8新特性之交互式通知的更多相关文章

  1. iOS8新特性(1)——UIAlertController

    一.iOS8介绍 iOS8 新特性,主要是UI上进行了统一 1.UIAlertController 2.UIPresentaionController:管理所有通过modal出来的控制器(看笔记) 3 ...

  2. iOS8 新特性

    iOS8新特性主要体现在4方面 1.UIAlertController 对alert&actionSheet的封装 UIAlertController.h 提示框按钮的选择 typedef N ...

  3. ios8新特性widget开发-b

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

  4. iOS8新特性

    1. App Extension Programming Guide 2.LocalAuthentication.framework - Touch ID Authentication 3.Local ...

  5. iOS8新特性之基于地理位置的消息通知UILocalNotification

              苹果在WWDC2014上正式公布了全新的iOS8操作系统. 界面上iOS8与iOS7相比变化不大,只是在功能方面进行了完好.                             ...

  6. iOS8新特性(1)-UIPopoverPresentationController使用

    从iOS 8开始,苹果提出新的 UIPopoverPresentationController代替UIPopoverController: 新的UIPopoverPresentationControl ...

  7. 利用iOS8新特性计算cell的实际高度

    在计算cell的实际高度是 我们一般是通过计算frame  拿到最底部一个控件的最大Y值从而的到cell 的高度  算来算去  比较麻烦 其实,iOS8已经提供了直接通过Cell高度自适应的方法了,根 ...

  8. iOS iOS8新特性--UIPopoverPresentationController

    1.回顾UIPopoverController的使用,下面这份代码只能在ipad下运行 // 初始化控制器,SecondViewController类继承自UIViewController Secon ...

  9. Ios8新特性-应用程序扩展

    一.什么是应用程序扩展? 应用程序扩展不是一个应用,它是主体应用程序(containing app)中一个单独的包,并能生成单独的二进制文件供其他应用调用. 个人感觉,类似于WP中的启动器,把系统当个 ...

随机推荐

  1. Dalvik虚拟机和JVM的对比

    Dalvik虚拟机与Java虚拟机有着很多相似的特性,都支持GC,JIT,JNI等等.其主要区别在于文件格式以及指令集不同,下面对两者的特性进行比较与讨论. Difference1:文件格式 Dalv ...

  2. BZOJ1010: [HNOI2008]玩具装箱toy(dp+斜率优化)

    Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 12451  Solved: 5407[Submit][Status][Discuss] Descript ...

  3. wordpress插件推荐

    以下插件可以全部到后台插件中心安装,只需使用关键词搜索即可 安全插件:Wordfence Security 后台增加一道密码:Stealth Login Page 隐藏后台登录地址:WPS-Hide- ...

  4. python 编码问题解决方案

    1.UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 0: ordinal not in range(128) ...

  5. C# 1.将整个文件夹复制到目标文件夹中 2.将指定文件复制到指定目标文件夹中

    ].Items.Clear(); string filePath = Application.StartupPath; string sourcePath = Path.Combine(filePat ...

  6. ZBrush带你发掘脸部雕刻的秘诀(上)

    骨骼,是一门基础艺术,几百年来一直为伟大的艺术大师所研究,它曾经,也将一直是创作现实且可信角色的关键,提高骨骼知识更将大大提高雕刻技能. 当然,这对于现实角色很重要,对卡通和风格化的角色也同样重要,底 ...

  7. pc页面滚动的时候,背景图不动只是页面滚动

    代码如下,直接拷贝出去就能看效果: 用到的方法 background-attachment 属性设置背景图像是否固定或者随着页面的其余部分滚动. <!DOCTYPE html> <h ...

  8. bootstrap中container 类和container-fluid类的区别container类所谓的自适应也是通过margin的改变来完成,container-fluid类的百分百宽度是指在固有的15px的padding前提下宽度总是当前视口的宽度。

    container 类和container-fluid类的区别体现在是否有随视口宽度改变的margin存在. container类所谓的自适应也是通过margin的改变来完成,container-fl ...

  9. RabbitMQ基础知识(转载)

    RabbitMQ基础知识(转载) 一.背景 RabbitMQ是一个由erlang开发的AMQP(Advanced Message Queue )的开源实现.AMQP 的出现其实也是应了广大人民群众的需 ...

  10. JS棋盘

    有一个棋盘,有64个方格,在第一个方格里面放1粒芝麻重量是0.00001kg, 第二个里面放2粒,第三个里面放4,棋盘上放的所有芝麻的重量 <!DOCTYPE html> <html ...