您的位置:首页 > 移动开发 > IOS开发

iOS 通知中心快速回复

2016-05-27 15:19 549 查看
iOS8拥有了全新的通知中心,有全新的通知机制。当屏幕顶部收到推送时只需要往下拉,就能看到快速操作界面,并不需要进入该应用才能操作。在锁屏界面,对于推送项目也可以快速处理。基本上就是让用户尽量在不离开当前页面的前提下处理推送信息,再次提高处理效率。
能够进行直接互动的短信、邮件、日历、提醒,第三方应用,可以让你不用进入程序就能进行快捷操作,并专注于手中正在做的事情,用户可以做如下操作:

在通知横幅快速回复信息,不用进入短信程序;
可直接拒绝或接受邮件邀请;
可对提醒进行标记为完成或推迟;
当第三方应用更新接口后便可直接对应用进行快速操作.

最常见的短信的快速回复,以下面的一条广告短信为例说明下如何使用快速回复的功能,具体如下所示:







最近在网上找了点资料,对这个功能进行了一下简单的实现,具体的步骤如下:

创建步骤

AppDelegate
中的
didFinishLaunchingWithOptions
,按照下面的步骤加入各个代码片段:
1.创建消息上要添加的动作,以按钮的形式显示
//接受按钮
UIMutableUserNotificationAction *acceptAction = [[UIMutableUserNotificationAction alloc] init];
acceptAction.identifier = @"acceptAction";
acceptAction.title = @"接受";
acceptAction.activationMode = UIUserNotificationActivationModeForeground;
//拒绝按钮
UIMutableUserNotificationAction *rejectAction = [[UIMutableUserNotificationAction alloc] init];
rejectAction.identifier = @"rejectAction";
rejectAction.title = @"拒绝";
rejectAction.activationMode = UIUserNotificationActivationModeBackground;
rejectAction.authenticationRequired = YES;//需要解锁才能处理,如果action.activationMode = UIUserNotificationActivationModeForeground;则这个属性被忽略;
rejectAction.destructive = YES;

2.创建动作(按钮)的类别集合
UIMutableUserNotificationCategory *categorys = [[UIMutableUserNotificationCategory alloc] init];
categorys.identifier = @"alert";
NSArray *actions = @[acceptAction, rejectAction];
[categorys setActions:actions forContext:UIUserNotificationActionContextMinimal];

3.创建UIUserNotificationSettings,并设置消息的显示类型
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound) categories:[NSSet setWithObjects:categorys, nil]];

4.注册推送
[[UIApplication sharedApplication] registerForRemoteNotifications];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];

在使用Push的时候需要在数据包中加入特定的Category字段(字段内容需要前后端定义为一致),终端接收到到后,就能展示上述代码对应Category设置的按钮,和响应按钮事件。
{"aps":{"alert":"测试推送的快捷回复", "sound":"default", "badge": 1, "category":"alert"}}

说明:
Push数据包之前能带的数据最多为256字节,现在APPLE将该数值放大到2KB。

按钮处理事件

1.以本地通知为例介绍怎么处理,如下代码是发起本地通知事件:
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.alertBody = @"测试推送的快捷回复";
notification.category = @"alert";
[[UIApplication sharedApplication] scheduleLocalNotification:notification];

2.实现下面的Delegate方法
(1)成功注册registerUserNotificationSettings后回调的方法
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
NSLog(@"%@", notificationSettings);
}

(2)事件处理
-(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler
{
//在非本App界面时收到本地消息,下拉消息会有快捷回复的按钮,点击按钮后调用的方法,根据identifier来判断点击的哪个按钮,notification为消息内容
NSLog(@"%@----%@",identifier,notification);
//处理完消息,最后一定要调用这个代码块
completionHandler();
}

运行之后要按
shift + command + H
,让程序推到后台,或者按
command+L
让模拟器锁屏,才会看到效果!如果是程序退到后台了,收到消息后下拉消息,则会出现刚才添加的两个按钮;如果是锁屏了,则出现消息后,左划就会出现刚才添加的两个按钮。

最近研究了下iOS8的官方文档,对这项功能进行了钻研,基本上效果已经出来。因为远程消息推送比较繁琐,需要后台支持,所以我用本地推送来代替的,基本上它们要调用的代理方法类似。长话短说,下面我就说下基本的流程:
1.创建消息上面要添加的动作(按钮的形式显示出来)

[objc] view
plain copy







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.创建动作(按钮)的类别集合

[objc] view
plain copy







UIMutableUserNotificationCategory *categorys = [[UIMutableUserNotificationCategory alloc] init];

categorys.identifier = @"alert";//这组动作的唯一标示

[categorys setActions:@[action,action2] forContext:(UIUserNotificationActionContextMinimal)];

3.创建UIUserNotificationSettings,并设置消息的显示类类型

[objc] view
plain copy







UIUserNotificationSettings *uns = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound) categories:[NSSet setWithObjects:categorys, nil nil]];

4.注册推送

[objc] view
plain copy







[[UIApplication sharedApplication] registerUserNotificationSettings:uns];

<pre name="code" class="objc">[[UIApplication sharedApplication] registerForRemoteNotifications];

UserRequires call to registerUserNotificationSettings:• SilentInfo.plist UIBackgroundModes array contains remote-notification•
Can use both
离线push数据包带上特定Category字段(字段内容需要前后台一起定义,必须要保持一致),手机端收到时,就能展示上述代码对应Category设置的按钮,和响应按钮事件。
// payload example: {"aps":{"alert":"Incoming call", "sound":"default", "badge": 1, "category":"incomingCall"}}
重大修改: 离线push数据包之前能带的数据最多为256字节,现在APPLE将该数值放大到2KB。 这个应该是只针对IOS8的。
5.发起本地推送消息

[objc] view
plain copy







UILocalNotification *notification = [[UILocalNotification alloc] init];

notification.fireDate=[NSDate dateWithTimeIntervalSinceNow:5];

notification.timeZone=[NSTimeZone defaultTimeZone];

notification.alertBody=@"测试推送的快捷回复";

notification.category = @"alert";

[[UIApplication sharedApplication] scheduleLocalNotification:notification];

//用这两个方法判断是否注册成功

// NSLog(@"currentUserNotificationSettings = %@",[[UIApplication sharedApplication] currentUserNotificationSettings]);

//[[UIApplication sharedApplication] isRegisteredForRemoteNotifications];

6.在AppDelegate.m里面对结果进行处理

[objc] view
plain copy







//本地推送通知

-(void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings

{

//成功注册registerUserNotificationSettings:后,回调的方法

NSLog(@"%@",notificationSettings);

}

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification

{

//收到本地推送消息后调用的方法

NSLog(@"%@",notification);

}

-(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler

{

//在非本App界面时收到本地消息,下拉消息会有快捷回复的按钮,点击按钮后调用的方法,根据identifier来判断点击的哪个按钮,notification为消息内容

NSLog(@"%@----%@",identifier,notification);

completionHandler();//处理完消息,最后一定要调用这个代码块

}

//远程推送通知

-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken

{

//向APNS注册成功,收到返回的deviceToken

}

-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error

{

//向APNS注册失败,返回错误信息error

}

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo

{

//收到远程推送通知消息

}

-(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler

{

//在没有启动本App时,收到服务器推送消息,下拉消息会有快捷回复的按钮,点击按钮后调用的方法,根据identifier来判断点击的哪个按钮

}

运行之后要按shift + command +H,让程序推到后台,或者按command+L让模拟器锁屏,才会看到效果!
如果是程序退到后台了,收到消息后下拉消息,则会出现刚才添加的两个按钮;如果是锁屏了,则出现消息后,左划就会出现刚才添加的两个按钮。
效果如下:



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: