14.UILocalNotification

在iOS中有两类信息提示推送方式,一类是远程服务器推送(APNS),还有一类就是本地通知UILocalNotification

UILocalNotification的属性有fireDate、timeZone、repeatInterval、repeatCalendar、alertBody、alertAction、hasAction、alertLaunchImage、applicationIconBadgeNumber、soundName和userInfo。

UILocalNotification的调度

其中fireDate、timeZone、repeatInterval和repeatCalendar是用于UILocalNotification的调度。fireDate是UILocalNotification的激发的确切时间。timeZone是UILocalNotification激发时间是否根据时区改变而改变,如果设置为nil的话,那么UILocalNotification将在一段时候后被激发,而不是某一个确切时间被激发。repeatInterval是UILocalNotification被重复激发之间的时间差,不过时间差是完全根据日历单位(NSCalendarUnit)的,例如每周激发的单位,NSWeekCalendarUnit,如果不设置的话,将不会重复激发。repeatCalendar是UILocalNotification重复激发所使用的日历单位需要参考的日历,如果不设置的话,系统默认的日历将被作为参考日历。

UILocalNotification的提醒内容

alertBody、alertAction、hasAction和alertLaunchImage是当应用不在运行时,系统处理

 

UILocalNotification提醒是需要的内容。alertBody是一串现实提醒内容的字符串(NSString),如果alertBody未设置的话,Notification被激发时将不现实提醒。alertAction也是一串字符(NSString),alertAction的内容将作为提醒中动作按钮上的文字,如果未设置的话,提醒信息中的动作按钮将显示为“View”相对文字形式。alertLaunchImage是在用户点击提醒框中动作按钮(“View”)时,等待应用加载时显示的图片,这个将替代应用原本设置的加载图片。hasAction是一个控制是否在提醒框中显示动作按钮的布尔值,默认值为YES。


 

UILocalNotification的其他部分

applicationIconBadgeNumber、soundName和userInfo将使UILocalNotification更完整。applicationIconBadgeNumber是显示在应用图标右上角的数字,这样让用户直接了解到应用需要处理的Notification。soundName是另一个UILocalNotification用来提醒用户的手段,在Notification被激发之后将播放这段声音来提醒用户有Notification需要处理,如果不设soundName的话,Notification被激发是将不会有声音播放,除去应用特制的声音以外,也可以将soundName设为UILocalNotificationDefaultSoundName来使用系统默认提醒声音。userInfo是Notification用来传递数据的NSDictionary。


登记UILocalNotification

在设置完UILocalNotification对象之后,应用需要在系统Notification处理队列中登记已设置完的UILocalNotification对象。登记UILocalNotification * localNotification的方式为:

   [[UIApplication sharedApplication]  scheduleLocalNotification:localNotification];

在有些时候,应用可能需要直接激发一个Notification而不是等一段时间在激发,应用可以以下的方式直接触发已设好的Notification:

   [[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];

处理UILocalNotification

在提醒框动作按钮被点击后,应用开始运行时,可以在-(BOOL)application:didFinishLaunchingWithOptions:这个Application delegate方法中处理。可以通过以下方式来加载为最近未处理的Notification:

   UILocalNotification * localNotif=[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];

如果应用正在运行时,可以通过覆盖在Application Delegate中的方法-(void)application:didReceiveLocalNotification:来处理Notification。作为方法的第二个参数为UILocalNotification对象,只需处理对象携带的userInfo来处理响应的动作。

取消UILocalNotification

可以使用以下两个方式来取消一个已经登记的Notification,第一个方式可以直接取消一个指定的Notification,第二个方式将会把该应用已登记的Notification一起取消

   [[UIApplication sharedApplication] cancelLocalNotification:localNotification];

   [[UIApplication sharedApplication] cancelAllLocalNotification];


例子实现1:

1、增加一个本地推送

//设置20秒之后 

NSDate *date = [NSDate dateWithTimeIntervalSinceNow:20];

    //chuagjian一个本地推送
    UILocalNotification *noti = [[[UILocalNotification alloc] init] autorelease];
    if (noti) {
        //设置推送时间
        noti.fireDate = date;
        //设置时区
        noti.timeZone = [NSTimeZone defaultTimeZone];
        //设置重复间隔
        noti.repeatInterval = NSWeekCalendarUnit;
        //推送声音
        noti.soundName = UILocalNotificationDefaultSoundName;
        //内容
        noti.alertBody = @"推送内容";
        //显示在icon上的红色圈中的数子
        noti.applicationIconBadgeNumber = 1;
        //设置userinfo 方便在之后需要撤销的时候使用
        NSDictionary *infoDic = [NSDictionary dictionaryWithObject:@"name" forKey:@"key"];
        noti.userInfo = infoDic;
        //添加推送到uiapplication        
        UIApplication *app = [UIApplication sharedApplication];
        [app scheduleLocalNotification:noti];  
    }
2、程序运行时接收到本地推送消息
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification*)notification
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"接收到本地提醒 in app"
message:notification.alertBody
   delegate:nil
  cancelButtonTitle:@"确定"
  otherButtonTitles:nil];
[alert show];
//这里,你就可以通过notification的useinfo,干一些你想做的事情了
application.applicationIconBadgeNumber -= 1;
}
3、取消一个本地推送
UIApplication *app = [UIApplication sharedApplication];
    //获取本地推送数组
    NSArray *localArr = [app scheduledLocalNotifications];
    //声明本地通知对象
    UILocalNotification *localNoti;
    if (localArr) {
        for (UILocalNotification *noti in localArr) {
            NSDictionary *dict = noti.userInfo;
            if (dict) {
                NSString *inKey = [dict objectForKey:@"key"];
                if ([inKey isEqualToString:key]) {
                    if (localNoti){
                        [localNoti release];
                        localNoti = nil;
                    }
                    localNoti = [noti retain];
                    break;
                }
            }
        }
        //判断是否找到已经存在的相同key的推送
        if (!localNoti) {
            //不存在 初始化
            localNoti = [[UILocalNotification alloc] init];
        }
        if (localNoti && !state) {
            //不推送 取消推送
            [app cancelLocalNotification:localNoti];
            [localNoti release];
            return;
        }
}

实例2

 
 
[cpp]  view plain copy
  1. UILocalNotification *notification = [[UILocalNotification alloc] init];  
  2.     NSDateFormatter *formatter = [[NSDateFormatter alloc] init];  
  3.     [formatter setDateFormat:@"HH:mm:ss"];  
  4.     //触发通知的时间  
  5.     NSDate *now = [formatter dateFromString:@"15:00:00"];  
  6.     notification.fireDate = now;  
  7.     //时区  
  8.     notification.timeZone = [NSTimeZone defaultTimeZone];  
  9.     //通知重复提示的单位,可以是天、周、月  
  10.     notification.repeatInterval = NSDayCalendarUnit;  
  11.     //通知内容  
  12.     notification.alertBody = @"这是一个新的通知";  
  13.     //通知被触发时播放的声音  
  14.     notification.soundName = UILocalNotificationDefaultSoundName;  
  15.     //执行通知注册  
  16.     [[UIApplication sharedApplication] scheduleLocalNotification:notification];  
实例3闹钟
主要代码:
       NSDate* now = [NSDate date];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
    NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    comps = [calendar components:unitFlags fromDate:now];
    int hour = [comps hour];
    int min = [comps minute];
    int sec = [comps second]; 
//--------------------------------------------取得系统的时间,并将其一个个赋值给变量---------------------

代码二:

UILocalNotification *notification=[[UILocalNotification alloc] init];
    if (notification!=nil) 
    {

        NSDate *now=[NSDate new];
        notification.fireDate=[now addTimeInterval:hm]; //设置响应时间,单位 秒
        NSLog(@"%d",hm);
        notification.timeZone=[NSTimeZone defaultTimeZone]; 
        notification.soundName = @"ping.caf";          //加入声音
        //notification.alertBody=@"TIME!";
        
        notification.alertBody = [NSString stringWithFormat:NSLocalizedString(@"你设置的时间是:%i : %i .",nil),htime1,mtime1];  //弹出对话框 
        [[UIApplication sharedApplication]   scheduleLocalNotification:notification];
    }

设置更多本地通知的信息:

  • 设置icon上数字。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {         // Override point for customization after application launch.      /    application.applicationIconBadgeNumber = 0;      // Add the view controller’s view to the window and display.      [self.window addSubview:viewController.view];      [self.window makeKeyAndVisible];

   

    return YES;  }

  • 添加通知时间,通知类型,取消通知

#pragma mark –  #pragma mark onChageValue  -(IBAction)onChangeValue:(id)sender  {      UISwitch *switch1=(UISwitch *)sender;      if (switch1.on) {          UILocalNotification *notification=[[UILocalNotification alloc] init];          NSDate *now1=[NSDate date];           notification.timeZone=[NSTimeZone defaultTimeZone];          notification.repeatInterval=NSDayCalendarUnit;          notification.applicationIconBadgeNumber = 1;          notification.alertAction = NSLocalizedString(@"显示", nil);          switch (switch1.tag) {              case 0:              {                  notification.fireDate=[now1 dateByAddingTimeInterval:10];                  notification.alertBody=self.myLable1.text;              }                  break;              case 1:              {                  notification.fireDate=[now1 dateByAddingTimeInterval:20];                  notification.alertBody=self.myLable2.text;              }                  break;              case 2:              {                  notification.fireDate=[now1 dateByAddingTimeInterval:30];                  notification.alertBody=self.myLable3.text;              }                  break;              default:                  break;          }          [notification setSoundName:UILocalNotificationDefaultSoundName];          NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:                                [NSString stringWithFormat:@"%d",switch1.tag], @"key1", nil];          [notification setUserInfo:dict];          [[UIApplication sharedApplication]   scheduleLocalNotification:notification];      }else {          NSArray *myArray=[[UIApplication sharedApplication] scheduledLocalNotifications];          for (int i=0; i<[myArray count]; i++) {              UILocalNotification    *myUILocalNotification=[myArray objectAtIndex:i];              if ([[[myUILocalNotification userInfo] objectForKey:@"key1"] intValue]==switch1.tag) {                  [[UIApplication sharedApplication] cancelLocalNotification:myUILocalNotification];              }          }      }  }



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值