iOS中 本地通知/本地通知详解

布局如下:(重点讲本地通知)

每日更新关注:http://weibo.com/hanjunqiang  新浪微博

Notification是智能手机应用编程中非常常用的一种传递信息的机制,而且可以非常好的节省资源,不用消耗资源来不停地检查信息状态(Pooling),在iOS下应用分为两种不同的Notification种类,本地和远程。本地的Notification由iOS下NotificationManager统一管理,只需要将封装好的本地Notification对象加入到系统Notification管理机制队列中,系统会在指定的时间激发将本地Notification,应用只需设计好处理Notification的方法就完成了整个Notification流程了。

本地Notification所使用的对象是UILocalNotificationUILocalNotification的属性涵盖了所有处理Notification需要的内容。UILocalNotification的属性有fireDate、timeZone、repeatInterval、repeatCalendar、alertBody、 alertAction、hasAction、alertLaunchImage、applicationIconBadgeNumber、 soundName和userInfo。


每日更新关注:http://weibo.com/hanjunqiang  新浪微博

1.首先要明白模拟器和真机的区别:模拟器不会有音频提示,另外就是没有检测允许接受通知,所以我补充一下几点:

1.添加监测通知:

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]){  
  2.          
  3.        [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];  
  4.    }  

上代码:

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #import "ViewController.h"  
  2. #import "DetailViewController.h"  
  3. @interface ViewController ()  
  4. @property (weak, nonatomic) IBOutlet UIButton *schedule;  
  5. @property (weak, nonatomic) IBOutlet UIButton *unSchedule;  
  6.   
  7. @end  
  8.   
  9. @implementation ViewController  
  10.   
  11. - (void)viewDidLoad {  
  12.     [super viewDidLoad];  
  13.     // Do any additional setup after loading the view, typically from a nib.  
  14. }  
  15.   
  16. // 调度通知  
  17. - (IBAction)schedule:(UIButton *)sender {  
  18.      
  19.     // 1.创建通知  
  20.     UILocalNotification *ln = [[UILocalNotification alloc]init];  
  21.       
  22.     if (ln) {  
  23.         // 设置时区  
  24.         ln.timeZone = [NSTimeZone defaultTimeZone];  
  25.         // 通知第一次发出的时间  
  26.         ln.fireDate = [[NSDate date]dateByAddingTimeInterval:5];  
  27.           
  28.         // 2.设置通知属性  
  29.         ln.soundName = @"click.wav"// 音效文件名  
  30.         // 通知的具体内容  
  31.         ln.alertBody = @"重大新闻:小韩哥的博客又更新了,赶快进来看看吧!....";  
  32.           
  33.         // 锁屏界面显示的小标题,完整标题:(“滑动来”+小标题)  
  34.         ln.alertAction = @"查看新闻吧";  
  35.           
  36.         // 设置app图标数字  
  37.         ln.applicationIconBadgeNumber = 10;  
  38.           
  39.         // 设置app的额外信息  
  40.         ln.userInfo = @{  
  41.                         @"icon":@"text.png",  
  42.                         @"title":@"重大新闻",  
  43.                         @"time":@"2016-02-28",  
  44.                         @"body":@"重大新闻:小韩哥的博客又更新了,赶快进来看看吧!"  
  45.                         };  
  46.         // 设置重启图片  
  47.         ln.alertLaunchImage = @"101339g76j7j9t2zgzdvkj.jpg";  
  48.          
  49.         // 设置重复发出通知的时间间隔  
  50. //        ln.repeatInterval = NSCalendarUnitMinute;  
  51.           
  52.         // 3.调度通知(启动任务,在规定的时间发出通知)  
  53.         [[UIApplication sharedApplication]scheduleLocalNotification:ln];  
  54.         // 直接发出通知没意义  
  55. //        [[UIApplication sharedApplication]presentLocalNotificationNow:ln];  
  56.     }  
  57.       
  58. }  
  59. - (IBAction)noSchedule:(UIButton *)sender  
  60. {  
  61. //    [[UIApplication sharedApplication]cancelAllLocalNotifications];  
  62.     // 已经发出且过期的通知会从数组里自动移除  
  63.     NSArray *notes = [UIApplication sharedApplication].scheduledLocalNotifications;  
  64.     NSLog(@"%@",notes);  
  65. }  
  66.   
  67.   
  68. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(UILocalNotification *)note  
  69. {  
  70.     DetailViewController *detailVC = segue.destinationViewController;  
  71.     detailVC.userInfo = note.userInfo;  
  72. }  
  73. @end  

2.通知详情页面设置基本属性:

每日更新关注:http://weibo.com/hanjunqiang  新浪微博

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. .h  
  2. #import <UIKit/UIKit.h>  
  3. @interface DetailViewController : UIViewController  
  4.   
  5. @property (nonatomicstrongNSDictionary *userInfo;  
  6. @end  
  7.   
  8.   
  9. .m  
  10. #import "DetailViewController.h"  
  11.   
  12. @interface DetailViewController ()  
  13. @property (weak, nonatomic) IBOutlet UILabel *userInfoContent;  
  14.   
  15. @end  
  16.   
  17. @implementation DetailViewController  
  18.   
  19. - (void)viewDidLoad {  
  20.     [super viewDidLoad];  
  21.      
  22.      self.userInfoContent.text = self.userInfo[@"body"];  
  23. }  
  24.   
  25. - (void)setUserInfo:(NSDictionary *)userInfo  
  26. {  
  27.     _userInfo = userInfo;  
  28. }  
  29. @end  

3. didFinishLaunchingWithOptions 实时监测:

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  
  2.     //注册本地通知  
  3.       
  4.     if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]){  
  5.           
  6.         [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];  
  7.     }  
  8.   
  9. //    NSLog(@"-----didFinishLaunchingWithOptions---");  
  10.     UILabel *label = [[UILabel alloc]init];  
  11.     label.frame = CGRectMake(064320100);  
  12.     label.backgroundColor = [UIColor redColor];  
  13.     label.font = [UIFont systemFontOfSize:11];  
  14.     label.numberOfLines = 0;  
  15.     label.textColor = [UIColor whiteColor];  
  16.     label.text = [launchOptions description];  
  17.     [[[self.window.rootViewController.childViewControllers firstObject] view]addSubview:label];  
  18.       
  19.     UILocalNotification *note = launchOptions[UIApplicationLaunchOptionsURLKey];  
  20.     if (note) {  
  21.         label.text = @"点击本地通知启动的程序";  
  22.     }else{  
  23.         label.text = @"直接点击app图标启动的程序";  
  24.     }  
  25.     self.label = label;  
  26.     return YES;  
  27. }  
  28. /** 
  29.  * 当用户点击本地通知进入app的时候调用(app当时并没有被关闭) 
  30.  * 若app已关闭不会被调用此方法 
  31.  */  
  32. - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification  
  33. {  
  34.     self.label.text = @"点击通知再次回到前台";  
  35.     ViewController *homeVC = [self.window.rootViewController.childViewControllers firstObject];  
  36. //    [homeVC performSegueWithIdentifier:@"toHome" sender:notification];  
  37.     [homeVC performSegueWithIdentifier:@"toHome" sender:notification];  
  38.       
  39. }  
三种情况展示:(重要)

每日更新关注:http://weibo.com/hanjunqiang  新浪微博

1.程序运行在后台

           

    

每日更新关注:http://weibo.com/hanjunqiang  新浪微博

Demo下载地址Github:  https://github.com/XiaoHanGe/LocalNotification



原文地址:http://blog.csdn.net/qq_31810357/article/details/50760917

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值