UILocalNotification的学习

示例代码:http://download.csdn.net/detail/chenscda/7230625

介绍一下iOS下如何使用UILocalNotification进行应用程序的本地通知,基本上大部分的app都会有这个功能。

     我们在设置的通知中心中可以自定义本地通知的三种形式(分别是在ios6和ios7):

                       

 

下面给出简单代码看看如何使用UILocalNotification:

(1)本地通知中心发送消息:

 

[cpp] view plaincopy
 
  1. UILocalNotification *notification=[[UILocalNotification alloc] init];  
  2.     if (notification!=nil) {  
  3.           
  4.         NSDate *now=[NSDate new];  
  5.         notification.fireDate=[now dateByAddingTimeInterval:6]; //触发通知的时间  
  6.         notification.repeatInterval=0; //循环次数,kCFCalendarUnitWeekday一周一次  
  7.           
  8.         notification.timeZone=[NSTimeZone defaultTimeZone];  
  9.         notification.soundName = UILocalNotificationDefaultSoundName;  
  10.         notification.alertBody=@"该去吃晚饭了!";  
  11.           
  12.         notification.alertAction = @"打开";  //提示框按钮  
  13.         notification.hasAction = YES; //是否显示额外的按钮,为no时alertAction消失  
  14.           
  15.         notification.applicationIconBadgeNumber = 1; //设置app图标右上角的数字  
  16.           
  17.         //下面设置本地通知发送的消息,这个消息可以接受  
  18.         NSDictionary* infoDic = [NSDictionary dictionaryWithObject:@"value" forKey:@"key"];  
  19.         notification.userInfo = infoDic;  
  20.         //发送通知  
  21.         [[UIApplication sharedApplication] scheduleLocalNotification:notification];  
  22.     }  


(2)接受本地通知发送的消息(在AppDelegate文件中)

 

 

[cpp] view plaincopy
 
  1. - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification*)notification{  
  2.       
  3.     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"LocalNotification" message:notification.alertBody delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];  
  4.     [alert show];  
  5.       
  6.     NSDictionary* dic = [[NSDictionary alloc]init];  
  7.     //这里可以接受到本地通知中心发送的消息  
  8.     dic = notification.userInfo;  
  9.     NSLog(@"user info = %@",[dic objectForKey:@"key"]);  
  10.       
  11.     // 图标上的数字减1  
  12.     application.applicationIconBadgeNumber -= 1;  
  13. }  
  14.                               
  15. - (void)applicationWillResignActive:(UIApplication *)application  
  16. {  
  17.     // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.  
  18.     // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.  
  19.       
  20.     // 图标上的数字减1  
  21.     application.applicationIconBadgeNumber -= 1;  
  22. }  


下面给出ios7下运行图标:

 

                   

 

 

这个内容比较简单,就不多说,详细的内容看看文档就可以了。大笑

 

posted on 2014-04-22 09:27  chenhanqing_blcu  阅读(222)  评论(0编辑  收藏  举报