IOS 的本地通知

- (void)viewDidLoad

{

[super viewDidLoad];

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

if(localNotification)

{

NSDate *now=[NSDate date];

localNotification.timeZone = [NSTimeZonedefaultTimeZone];

localNotification.repeatInterval =NSDayCalendarUnit;

localNotification.applicationIconBadgeNumber = 1;

localNotification.alertBody=@"该去吃晚饭了!";

localNotification.alertAction =NSLocalizedString(@"显示", nil);

NSDictionary* dic = [NSDictionarydictionaryWithObject:@"123" forKey:@"key"];

[localNotification setUserInfo:dic];

localNotification.fireDate = [nowdateByAddingTimeInterval:10];

//    localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];

localNotification.hasAction = YES;

//        localNotification.repeatInterval = NSMinuteCalendarUnit;

localNotification.soundName =UILocalNotificationDefaultSoundName;

[[UIApplication sharedApplication]scheduleLocalNotification:localNotification];

[localNotification release];

}

//得到(24 * 60 * 60)即24小时之前的日期,dateWithTimeIntervalSinceNow:

NSDate* yesterday = [NSDatedateWithTimeIntervalSinceNow:-(24 * 60 * 60)];

NSLog(@"yesterday:%@",yesterday);

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

[dateFormat setDateFormat:@"YYYY-MM-dd hh:mm:ss"];

//计算 两个时间相隔多少秒

NSDate* date1 = [dateFormat dateFromString:[dateFormatstringFromDate:[NSDatedateWithTimeIntervalSinceNow:60*60*8]]];

NSLog(@"date1 = %@",date1);

NSDate* date2 =[NSDate dateWithTimeInterval:60*60*8sinceDate:[dateFormat dateFromString:@"2013-10-12 11:08:33"]] ;

NSLog(@"date2 = %@",date2);

NSTimeInterval timerInterVal = [date2timeIntervalSinceDate:date1];

NSLog(@"timerInterVal  %f",timerInterVal);

}

//监听本地通知事件:退出程序后执行:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen]bounds]] autorelease];

// Override point for customization after application launch.

self.window.backgroundColor = [UIColor whiteColor];

ViewController* view = [[ViewController alloc] init];

[self.window setRootViewController:view];

[view release];

//设置iCon 上的数字

application.applicationIconBadgeNumber = 0;

UILocalNotification* notification = [launchOptionsobjectForKey:UIApplicationLaunchOptionsAnnotationKey];

if(notification)

{

NSLog(@"didFinishLaunchingWithOptions");

UIAlertView *alert =  [[UIAlertView alloc] initWithTitle:nilmessage:@"received E-mail" delegate:self cancelButtonTitle:@"OK"otherButtonTitles:nil];

[alert show];

[alert release];

}

[self.window makeKeyAndVisible];

return YES;

}

//监听本地通知事件:在没有退出程序的时候执行

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification

{

NSLog(@"%@",[notification.userInfo valueForKey:@"key"]);

if(application.applicationState == UIApplicationStateActive)

{

UIAlertView* alert = [[[UIAlertView alloc] initWithTitle:@""

message:@"事项提醒"

delegate:self

cancelButtonTitle:@"关闭"

otherButtonTitles:nil, nil] autorelease];

[alert show];

}

else

{

[[UIApplication sharedApplication] cancelAllLocalNotifications];

}

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

{

[[UIApplication sharedApplication] cancelAllLocalNotifications];

}

 

下面是写在类里面的

+(void)addLocalNotificationWithMessage:(NSString *)message

FireDate:(NSDate *) fireDate

AlarmKey:(NSString *)alarmKey

{

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

if (notification!=nil) {

notification.fireDate=fireDate;

notification.timeZone=[NSTimeZone defaultTimeZone];

notification.soundName= UILocalNotificationDefaultSoundName;

notification.alertBody=message;

notification.hasAction = NO;

notification.userInfo=[[NSDictionary alloc]initWithObjectsAndKeys:alarmKey,@"AlarmKey", nil];

[[UIApplication sharedApplication] scheduleLocalNotification:notification];

}

[notification release];

}

+(void)deleteLocalNotification:(NSString *) alarmKey

{

NSArray * allLocalNotification=[[UIApplication sharedApplication]scheduledLocalNotifications];

for (UILocalNotification * localNotification in allLocalNotification) {

NSString * alarmValue=[localNotification.userInfoobjectForKey:@"AlarmKey"];

if ([alarmKey isEqualToString:alarmValue]) {

[[UIApplication sharedApplication]cancelLocalNotification:localNotification];

}

}

}

最新文章

  1. 《锋利的jQuery》(第2版)读书笔记4
  2. awk命令简单介绍
  3. TF-IDF 简介
  4. 一位Erlang程序猿的自白
  5. 2013年arcgis培训
  6. c# DirectoryInfo类 详解
  7. 整整68页学习C++的文章
  8. [C++程序设计]有默认参数的函数
  9. Java 网络编程(四) InetAddress类
  10. iOS webservice接口soap协议调用遇到的问题
  11. 处理ftp服务器 在重启后ftp客户端不能连接访问的问题
  12. (网页)20个JS 小技巧超级实用
  13. POJ1962:Corporative Network【带权并查集】
  14. Android开源库项目集锦
  15. OSPF里几个特殊区域(stub、Totally stubby、NSSA、Totally NSSA)总结
  16. Metronic-最优秀的基于Bootstrap的响应式网站模版
  17. jdk1.8 HashMap源码讲解
  18. ZOJ 3955 Saddle Point
  19. 压测工具Siege
  20. PHP——0128练习相关2——js点击button按钮跳转到另一个新页面

热门文章

  1. heartbeat-gui
  2. Lucene TF-IDF 相关性算分公式
  3. 分布式文档存储数据库(MongoDB)副本集配置
  4. jsp选项卡导航实现——模板
  5. NNCRF之NNSegmentation, NNPostagging, NNNameEntity
  6. RNAseq 流程
  7. codeforces242E XOR on Segment
  8. lucene介绍和存储介绍
  9. Vue 备
  10. 1.JSON 转换对象失败问题 2.spring注入失效