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];
}
}
}