iOS8推送消息的快速回复处理

  • iOS8拥有了全新的通知中心,有全新的通知机制。当屏幕顶部收到推送时只需要往下拉,就能看到快速操作界面,并不需要进入该应用才能操作。在锁屏界面,对于推送项目也可以快速处理。基本上就是让用户尽量在不离开当前页面的前提下处理推送信息,再次提高处理效率。

    能够进行直接互动的短信、邮件、日历、提醒,第三方应用,可以让你不用进入程序就能进行快捷操作,并专注于手中正在做的事情。

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

    消息回复 消息快捷回复

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

    1.创建消息上面要添加的动作(按钮的形式显示出来)

     

    01. UIMutableUserNotificationAction *action = [[UIMutableUserNotificationAction alloc] init];
    02. action.identifier = @"action";//按钮的标示
    03. action.title=@"Accept";//按钮的标题
    04. action.activationMode = UIUserNotificationActivationModeForeground;//当点击的时候启动程序
    05. //    action.authenticationRequired = YES;
    06. //    action.destructive = YES;
    07.  
    08. UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc] init];  //第二按钮
    09. action2.identifier = @"action2";
    10. action2.title=@"Reject";
    11. action2.activationMode = UIUserNotificationActivationModeBackground;//当点击的时候不启动程序,在后台处理
    12. action.authenticationRequired = YES;//需要解锁才能处理,如果action.activationMode = UIUserNotificationActivationModeForeground;则这个属性被忽略;
    13. action.destructive = YES;

     

    2.创建动作(按钮)的类别集合

    1. UIMutableUserNotificationCategory *categorys = [[UIMutableUserNotificationCategory alloc] init];
    2. categorys.identifier = @"alert";//这组动作的唯一标示
    3. [categorys setActions:@[action,action2] forContext:(UIUserNotificationActionContextMinimal)];

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

     

    4.注册推送

    1. [[UIApplication sharedApplication] registerForRemoteNotifications];
    2. [[UIApplication sharedApplication] registerUserNotificationSettings:uns];

     

    5.发起本地推送消息

    01. UILocalNotification *notification = [[UILocalNotification alloc] init];
    02. notification.fireDate=[NSDate dateWithTimeIntervalSinceNow:5];
    03. notification.timeZone=[NSTimeZone defaultTimeZone];
    04. notification.alertBody=@"测试推送的快捷回复";
    05. notification.category = @"alert";
    06. [[UIApplication sharedApplication]  scheduleLocalNotification:notification];
    07.  
    08. //用这两个方法判断是否注册成功
    09. // NSLog(@"currentUserNotificationSettings = %@",[[UIApplication sharedApplication] currentUserNotificationSettings]);
    10. //[[UIApplication sharedApplication] isRegisteredForRemoteNotifications];

     

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

    01. //本地推送通知
    02. -(void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
    03. {
    04. //成功注册registerUserNotificationSettings:后,回调的方法
    05. NSLog(@"%@",notificationSettings);
    06. }
    07.  
    08. -(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
    09. {
    10. //收到本地推送消息后调用的方法
    11. NSLog(@"%@",notification);
    12. }
    13.  
    14. -(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler
    15. {
    16. //在非本App界面时收到本地消息,下拉消息会有快捷回复的按钮,点击按钮后调用的方法,根据identifier来判断点击的哪个按钮,notification为消息内容
    17. NSLog(@"%@----%@",identifier,notification);
    18. completionHandler();//处理完消息,最后一定要调用这个代码块
    19. }
    20.  
    21. //远程推送通知
    22. -(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
    23. {
    24. //向APNS注册成功,收到返回的deviceToken
    25. }
    26.  
    27. -(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
    28. {
    29. //向APNS注册失败,返回错误信息error
    30. }
    31.  
    32. -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
    33. {
    34. //收到远程推送通知消息
    35. }
    36.  
    37. -(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler
    38. {
    39. //在没有启动本App时,收到服务器推送消息,下拉消息会有快捷回复的按钮,点击按钮后调用的方法,根据identifier来判断点击的哪个按钮
    40. }


    运行之后要按shift + command +H,让程序推到后台,或者按command+L让模拟器锁屏,才会看到效果!

    如果是程序退到后台了,收到消息后下拉消息,则会出现刚才添加的两个按钮;如果是锁屏了,则出现消息后,左划就会出现刚才添加的两个按钮。

    效果如下:

    \ \

    现在只是能让消息中显示出按钮的形式,带输入框的还在研究中,如果大家研究出来了,也谢谢能分享一下啊,大家一起提高!

    代码:https://github.com/ios44first/PushDemo

    地址,谢谢 http://blog.csdn.net/yujianxiang666/article/details/35260135

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值