iOS8新特性之交互式通知

iOS8以前的远程通知想必大家都非常熟悉了,这里不做过多介绍,直接介绍iOS8中的交互式远程通知。

一、通过调用 [[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];
<strong><em>identifier</em></strong>
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();
    }
}
<strong><em>application</em></strong>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">收到通知的对象</span>

identifier

UIUserNotificationAction的唯一标示

userInfo

payload的内容

completionHandler

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

我测试用的paload是

{
    aps =     {
        alert = "Thank you very much";
        badge = 1;
        category = NotificationCategoryIdentifier;
        sound = "ping.caf";
    };
}
时间: 2025-01-08 10:07:47

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

iOS iOS8新特性-UIAlertController

iOS iOS8新特性--UIAlertController 1. iOS7及iOS7之前警告类控件有UIAlertView和UIActionSheet 1.1 UIAlertView的使用 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"警告" message:@"这是一个UIAlertView" delegate:nil cancelButtonTitle:@"取消" oth

iOS8新特性(1)——UIAlertController

一.iOS8介绍 iOS8 新特性,主要是UI上进行了统一 1.UIAlertController 2.UIPresentaionController:管理所有通过modal出来的控制器(看笔记) 3.UIPopoverPresentationController 4.SizeClass + Autolayout 5.App Extension 应用扩展 6.Core Image(iOS 5开始就有了,滤镜美图秀秀型 ) 二.UIAlertController 1.以往我们使用的时候,都是用的 

iOS8新特性扩展(Extension)应用之一——Today扩展

iOS8新特性扩展(Extension)应用之一--Today扩展 一.理解扩展 1.简介 基于iOS系统的安全性考虑,其应用的数据存储是通过沙盒模式进行的,要实现应用之间的数据共享十分困难,功能共享就更加棘手.在iOS8系统中,apple为我们提供了一个革命性的功能:扩展.我们可以通过扩展来使app间数据甚至功能进行共享. 2.几种扩展模式 (1)今日视图扩展:today 这个扩展也被叫做 widget.该扩展可以将今日发生的简短消息放到消息中心的「今日」视图里.这个功能类似于安卓系统中的小控

iOS8新特性扩展(Extension)应用之三——照片编辑插件

iOS8新特性扩展(Extension)应用之三--照片编辑插件 通过前几篇博客的介绍,我们了解到扩展给app提供的更加强大的交互能力,这种强大的交互能力另一方面体现在照片编辑插件的应用. 和通常一样,我们先创建一个工程,然后新建一个Target,选择photo editing: 从模板中,我们可以看到系统为我们创建了一个controller,这个controller就是用于处理照片的controller,其中方法如下: - (BOOL)canHandleAdjustmentData:(PHAd

【学习ios之路:UI系列】(UISearchBar,UISearchDisplayController) 和UISearchController(iOS8新特性)

1.UISearchBar(效果如下:) ①创建UISearchBar对象 //初始化,定义frame UISearchBar *bar = [[UISearchBar alloc] initWithFrame:CGRectMake (0, 50, self.view.frame.size.width, 80)]; //添加到控制器的视图上 [self.view addSubview:bar]; ②UISerachBar的属性 //autocapitalizationType:包含4种类型,但是

Ios8新特性-应用程序扩展

一.什么是应用程序扩展? 应用程序扩展不是一个应用,它是主体应用程序(containing app)中一个单独的包,并能生成单独的二进制文件供其他应用调用. 个人感觉,类似于WP中的启动器,把系统当个软件,启动器就是应用程序扩展: 二.与普通应用的关系 1.应用扩展不同于应用,但是需要基于应用去开发应用扩展,每个应用扩展都是独立于应用运行的二进制文件. 2.与主体应用程序不同,应用扩展实现的是一个特定的.狭义范围内的任务,并且要严格遵循扩展点的协议. 三.生命周期 1.生命周期开始于用户从某个应

iOS8新特性扩展(Extension)应用之二——分享插件

iOS8新特性扩展(Extension)应用之二--分享插件 在上一篇博客中,介绍了iOS8新特性扩展功能之一的Today功能:http://my.oschina.net/u/2340880/blog/485533,这里我们再介绍一下分享的扩展功能. 在iOS8之前,除了一些主流的社交平台,例如苹果支持内容分享外,其他开发者的应用若要加入分享的功能,将会十分的复杂.在iOS8的新特性中,apple为我们准备了这样的扩展功能. 首先创建工程,在我们的工程中新建一个Target: 之后,模板中会为我

iOS8新特性扩展(Extension)应用之四——自定义键盘控件

iOS8新特性扩展(Extension)应用之四--自定义键盘控件 iOS8系统的开放第三方键盘,使得用户在输入法的选择上更加自主灵活,也更加贴近不同语言的输入风格.这篇博客,将介绍如何开发一个第三方的键盘控件. 一.了解UIInputViewController类 UIInputViewController是系统扩展支持键盘扩展的一个类,通过这个类,我们可以自定义一款我们自己的键盘提供给系统使用. 首先,我们先来看一下这个类中的一些属性和方法: @property (nonatomic, re

ios8新特性widget开发-b

os8发布已经有一段时间了,伴随着ios8同时也出现了许多新的特性,ios系统将会越来越开放,这是好事.其中一个新特性就是在下拉通知栏里加入了个性的widget,开发者可以自己定义widget的样式内容.当然这个功能在Android上早就实现了,^_^.........,如下图: 首先先说几个概念, 1.app extension:extension是iOS8新开放的一种对几个固定系统区域的扩展机制,extension并不是一个独立的app,它有一个包含在app bundle中的独立bundle