IOS 的本地通知

 IOS 的本地通知 

- (void)viewDidLoad

{

    [super viewDidLoad];

   

    UILocalNotification* localNotification = [[UILocalNotification allocinit];

    

    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 allocinitWithFrame:[[UIScreen mainScreen]bounds]] autorelease];

    // Override point for customization after application launch.

    self.window.backgroundColor = [UIColor whiteColor];

    ViewController* view = [[ViewController allocinit];

    [self.window setRootViewController:view];

    [view release];

    

    

    //设置iCon 上的数字

    application.applicationIconBadgeNumber = 0;

    UILocalNotification* notification = [launchOptionsobjectForKey:UIApplicationLaunchOptionsAnnotationKey];    

    if(notification)

    {

        NSLog(@"didFinishLaunchingWithOptions");

        UIAlertView *alert =  [[UIAlertView allocinitWithTitle: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 allocinitWithTitle:@""

                                                         message:@"事项提醒"

                                                        delegate:self

                                               cancelButtonTitle:@"关闭"

                                               otherButtonTitles:nilnilautorelease];

        [alert show];

    }

    else

    {

 

        [[UIApplication sharedApplicationcancelAllLocalNotifications];

 

    }

}

 

 

 

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

{

    

        [[UIApplication sharedApplicationcancelAllLocalNotifications];

    

}

 

 

下面是写在类里面的

 

 

+(void)addLocalNotificationWithMessage:(NSString *)message

                              FireDate:(NSDate *) fireDate

                              AlarmKey:(NSString *)alarmKey

{

    UILocalNotification *notification=[[UILocalNotification allocinit];

    if (notification!=nil) {

        

        notification.fireDate=fireDate;

        

        notification.timeZone=[NSTimeZone defaultTimeZone];

        notification.soundNameUILocalNotificationDefaultSoundName;

        

        notification.alertBody=message;

        notification.hasAction = NO;

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

        [[UIApplication sharedApplicationscheduleLocalNotification: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];

        }

    }

}

posted @ 2013-12-26 07:46  光光96  阅读(285)  评论(0编辑  收藏  举报