http://blog.csdn.net/yujianxiang666/article/details/35260135ios
iOS8拥有了全新的通知中心,有全新的通知机制。当屏幕顶部收到推送时只须要往下拉,就能看到快速操做界面,并不须要进入该应用才能操做。在锁屏界面,对于推送项目也能够快速处理。基本上就是让用户尽可能在不离开当前页面的前提下处理推送信息,再次提升处理效率。git
可以进行直接互动的短信、邮件、日历、提醒,第三方应用,可让你不用进入程序就能进行快捷操做,并专一于手中正在作的事情。github
- 在通知横幅快速回复信息,不用进入短信程序;
- 可直接拒绝或接受邮件邀请;
- 可对提醒进行标记为完成或推迟;
- 当第三方应用更新接口后即可直接对应用进行快速操做。

最近研究了下iOS8的官方文档,对这项功能进行了钻研,基本上效果已经出来。由于远程消息推送比较繁琐,须要后台支持,因此我用本地推送来代替的,基本上它们要调用的代理方法相似。长话短说,下面我就说下基本的流程:xcode
1.建立消息上面要添加的动做(按钮的形式显示出来) 服务器
- UIMutableUserNotificationAction *action = [[UIMutableUserNotificationAction alloc] init];
- action.identifier = @"action";
- action.title=@"Accept";
- action.activationMode = UIUserNotificationActivationModeForeground;
-
-
-
- UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc] init];
- action2.identifier = @"action2";
- action2.title=@"Reject";
- action2.activationMode = UIUserNotificationActivationModeBackground;
- action.authenticationRequired = YES;
- action.destructive = YES;
2.建立动做(按钮)的类别集合app
- UIMutableUserNotificationCategory *categorys = [[UIMutableUserNotificationCategory alloc] init];
- categorys.identifier = @"alert";
- [categorys setActions:@[action,action2] forContext:(UIUserNotificationActionContextMinimal)];
3.建立UIUserNotificationSettings,并设置消息的显示类类型ide
- UIUserNotificationSettings *uns = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound) categories:[NSSet setWithObjects:categorys, nil nil]];
4.注册推送测试
-
- [[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 bothui
离线push数据包带上特定Category字段(字段内容须要先后台一块儿定义,必需要保持一致),手机端收到时,就能展现上述代码对应Category设置的按钮,和响应按钮事件。spa
// payload example: {"aps":{"alert":"Incoming call", "sound":"default", "badge": 1, "category":"incomingCall"}}
重大修改: 离线push数据包以前能带的数据最多为256字节,如今APPLE将该数值放大到2KB。 这个应该是只针对IOS8的。
5.发起本地推送消息
- UILocalNotification *notification = [[UILocalNotification alloc] init];
- notification.fireDate=[NSDate dateWithTimeIntervalSinceNow:5];
- notification.timeZone=[NSTimeZone defaultTimeZone];
- notification.alertBody=@"测试推送的快捷回复";
- notification.category = @"alert";
- [[UIApplication sharedApplication] scheduleLocalNotification:notification];
-
-
-
-
6.在AppDelegate.m里面对结果进行处理
- -(void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
- {
-
- NSLog(@"%@",notificationSettings);
- }
-
- -(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
- {
-
- NSLog(@"%@",notification);
- }
-
- -(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler
- {
-
- NSLog(@"%@----%@",identifier,notification);
- completionHandler();
- }
-
- -(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
- {
-
- }
-
- -(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
- {
-
- }
-
- -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
- {
-
- }
-
- -(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler
- {
-
- }
运行以后要按shift + command +H,让程序推到后台,或者按command+L让模拟器锁屏,才会看到效果!
若是是程序退到后台了,收到消息后下拉消息,则会出现刚才添加的两个按钮;若是是锁屏了,则出现消息后,左划就会出现刚才添加的两个按钮。
效果以下:

如今只是能让消息中显示出按钮的形式,带输入框的还在研究中,若是你们研究出来了,也谢谢能分享一下啊,你们一块儿提升!
代码:https://github.com/ios44first/PushDemo
(如要转载请注明地址,谢谢 http://blog.csdn.net/yujianxiang666/article/details/35260135)