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

  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

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

最新文章

  1. gradle基础的build文件模板_tomcat
  2. 开始VS 2012 中LightSwitch系列的第2部分:感受关爱——定义数据关系
  3. SVN的简单使用
  4. MFC单文档程序结构
  5. MyEclipse里项目部署到tomcat上之后,tomcat webpps文件夹里为什么找不到这个项目
  6. 第三代搜索推出网民评价系统,seo末日还会远吗?
  7. 关于Oracle dmp文件导入随笔
  8. android 如何加入第一3正方形lib图书馆kernel于
  9. ural1628 White Streaks
  10. 当配置 DispatcherServlet拦截“/”,SpringMVC访问静态资源的三种方式
  11. iframe父子操作
  12. image-to-image translation with conditional adversarial networks文献笔记
  13. Java-二进制转10进制原理机制
  14. sqlserver 操作数据表语句模板
  15. C++ Primer 笔记——函数
  16. linux查找大文件命令
  17. input属性type为file打开文件资源管理器时,如何限制多次选取或只能一次选取的行为
  18. cakephp引入其他控制器封装方法
  19. Python 循环的技巧
  20. node升级 npm的升级

热门文章

  1. bind()函数的作用
  2. chrome打开控制台状态下,没有人为打断点,自动进入断点模式的解决方法
  3. [Codeforces]Good Bye 2017
  4. 洛谷P1208 [USACO1.3]混合牛奶 Mixing Milk(贪心)
  5. javascript中构造函数知识总结
  6. android黑科技系列——实现静态的默认安装和卸载应用
  7. List 常用方法解析
  8. 关于生sql中的空值
  9. redis实现集群加主从复制
  10. Linux 文件压缩