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

    localNotification.alertBody = @"this is a local notification";

    localNotification.soundName = UILocalNotificationDefaultSoundName;

 

  //schedule the time to push local notification

    NSCalendar* calendar = [NSCalendar autoupdatingCurrentCalendar];

    NSDate* currentDate = [NSDate date];

    NSDateComponents* currentDateComponents = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit fromDate:currentDate];

    NSDateComponents* scheduledDateComponents = currentDateComponents;

 

    if (currentDateComponents != nil) {

        [scheduledDateComponents setDay:currentDateComponents.day + 7];

        [scheduledDateComponents setHour:17];

        [scheduledDateComponents setMinute:0];

        [scheduledDateComponents setSecond:0];

    }

  

    NSDate* scheduledDate = [calendar dateFromComponents:scheduledDateComponents];

    localNotification.fireDate = scheduledDate;

    

    [[UIApplication sharedApplication]cancelAllLocalNotifications];

    [[UIApplication sharedApplication]scheduleLocalNotification:localNotification];

 

 

 

/////SIMPLE WAY

http://stackoverflow.com/questions/10060578/uilocalnotification-fires-immediately-instead-of-at-scheduled-time

UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:1];
localNotification.alertBody = @"Your alert message";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
posted on 2015-04-20 11:14  ximenchuixie  阅读(218)  评论(0编辑  收藏  举报