iOS8 本地通知基本用法

1、本地通知的定义和使用:


本地通知是UILocalNotification的实例,主要有三类属性:
scheduled time,时间周期,用来指定iOS系统发送通知的日期和时间;
notification type,通知类型,包括警告信息、动作按钮的标题、应用图标上的badge(数字标记)和播放的声音;
自定义数据,本地通知可以包含一个dictionary类型的本地数据。
对本地通知的数量限制,iOS最多允许最近本地通知数量是64个,超过限制的本地通知将被iOS忽略。

    在iOS8 之前的本地通知是这样用的:

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. UILocalNotification *localNotification = [[UILocalNotification alloc] init];  
  2.     if (localNotification == nil) {  
  3.         return;  
  4.     }  
  5.     //设置本地通知的触发时间(如果要立即触发,无需设置),这里设置为20妙后  
  6.     localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:20];  
  7.     //设置本地通知的时区  
  8.     localNotification.timeZone = [NSTimeZone defaultTimeZone];  
  9.     //设置通知的内容  
  10.     localNotification.alertBody = affair.title;  
  11.     //设置通知动作按钮的标题  
  12.     localNotification.alertAction = @"查看”; 
  13.     //设置提醒的声音,可以自己添加声音文件,这里设置为默认提示声 
  14.     localNotification.soundName = UILocalNotificationDefaultSoundName; 
  15.     //设置通知的相关信息,这个很重要,可以添加一些标记性内容,方便以后区分和获取通知的信息 
  16.     NSDictionary *infoDic = [NSDictionary dictionaryWithObjectsAndKeys:LOCAL_NOTIFY_SCHEDULE_ID,@"id",[NSNumber numberWithInteger:time],@"time",[NSNumber numberWithInt:affair.aid],@"affair.aid", nil nil];  
  17.     localNotification.userInfo = infoDic;  
  18.     //在规定的日期触发通知  
  19.     [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];  
  20.       
  21.     //立即触发一个通知  
  22. //    [[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];  
  23.     [localNotification release];  



2、取消本地通知:

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //取消某一个通知  
  2.     NSArray *notificaitons = [[UIApplication sharedApplication] scheduledLocalNotifications];  
  3.     //获取当前所有的本地通知  
  4.     if (!notificaitons || notificaitons.count <= 0) {  
  5.         return;  
  6.     }  
  7.     for (UILocalNotification *notify in notificaitons) {  
  8.         if ([[notify.userInfo objectForKey:@"id"] isEqualToString:LOCAL_NOTIFY_SCHEDULE_ID]) {  
  9.             //取消一个特定的通知  
  10.             [[UIApplication sharedApplication] cancelLocalNotification:notify];  
  11.             break;  
  12.         }  
  13.     }  
  14.       
  15.     //取消所有的本地通知  
  16.     [[UIApplication sharedApplication] cancelAllLocalNotifications];  



3、本地通知的响应:

如果已经注册了本地通知,当客户端响应通知时:
     a、应用程序在后台的时候,本地通知会给设备送达一个和远程通知一样的提醒,提醒的样式由用户在手机设置中设置
     b、应用程序正在运行中,则设备不会收到提醒,但是会走应用程序delegate中的方法:

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {  
  2.       
  3. }  

,如果你想实现程序在后台时候的那种提醒效果,可以在上面这个方法中添加相关代码,示例代码:

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. if ([[notification.userInfo objectForKey:@"id"] isEqualToString:@"affair.schedule"]) {  
  2.         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"test" message:notification.alertBody delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:notification.alertAction, nil nil];  
  3.         [alert show];  
  4.     }  

需要注意的是,在情况a中,如果用户点击提醒进入应用程序,也会执行收到本地通知的回调方法,这种情况下如果你添加了上面那段代码,则会出现连续出现两次提示,为了解决这个问题,修改代码如下:

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. if ([[notification.userInfo objectForKey:@"id"] isEqualToString:@"affair.schedule"]) {  
  2.         //判断应用程序当前的运行状态,如果是激活状态,则进行提醒,否则不提醒  
  3.         if (application.applicationState == UIApplicationStateActive) {  
  4.             UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"test" message:notification.alertBody delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:notification.alertAction, nil nil];  
  5.             [alert show];  
  6.         }  
  7.     }  

    但是在iOS8之后需要用本地通知的时候要先进行注册通知,这样的:

//0.注册本地通知

    UIUserNotificationType type=UIUserNotificationTypeAlert;//顶部弹窗模式

    UIUserNotificationSettings *settings=[UIUserNotificationSettings settingsForTypes:type categories:nil];

    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];

    

    //1.创建本地通知对象

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

    

    //2.设置通知的属性

//    ln.soundName = @"buyao.wav";//音效文件(不设置此项时,声音为系统默认滴声)

    ln.soundName = UILocalNotificationDefaultSoundName;

    ln.alertBody = @"本地通知出现";

    

    //2.1设置时区

    ln.timeZone = [NSTimeZone defaultTimeZone];

    

    //2.2通知重复的频率设置

    ln.repeatInterval = NSCalendarUnitDay;//(设置为每天重复一次)

    

    //2.3通知第一次触发的时间(按钮点击5秒后触发)

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

    [formatter setDateFormat:@"HH:mm:ss"];

    

    int hour = 0;

    int mian = 0;

    int secound = 0;

    

    hour = (arc4random() % 12) + 9;//99+12之间的随机整数

    mian = arc4random() % 60;//060之间的随机整数、设置为分

    secound = arc4random() % 60;//060之间的随机整数、设置为秒


    NSString *string = [NSString stringWithFormat:@"%02d:%02d:%02d",hour,mian,secound];

    NSLog(@"----_%@",string);

    //2.4触发通知的时间

//    NSDate * now = [formatter dateFromString:string];

//    NSDate *now = [formatter dateFromString:@"09:51:00"];

    NSDate *now = [NSDate dateWithTimeIntervalSinceNow:5];

    ln.fireDate = now;


    //3.调度通知启动任务

    [[UIApplication sharedApplication] scheduleLocalNotification:ln];


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值