本地通知"每天上午7:00"没有通知

use*_*698 1 iphone xcode ipad ios uilocalnotification

我希望每天早上7点发出通知,但不会发生.我也想让它在锁屏中显示.这是我到目前为止的所有代码.

-(void)EveryDayNotify

{

    NSLog(@"Good Morning Working");

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


    [localNotification setAlertBody:@"Good Morning! Have a great day!"];
    localNotification.soundName = UILocalNotificationDefaultSoundName;
    localNotification.applicationIconBadgeNumber = 1;

    localNotification.repeatInterval = NSDayCalendarUnit;

    NSCalendar *calendar = [NSCalendar currentCalendar]; // gets default calendar
    NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:[NSDate date]]; // gets the year, month, day,hour and minutesfor today's date
    [components setHour:07];
    [components setMinute:0];

    localNotification.fireDate = [calendar dateFromComponents:components];


    [localNotification release];
}
Run Code Online (Sandbox Code Playgroud)

Mas*_*zer 7

试着用这个:

- (void)applicationDidEnterBackground:(UIApplication *)application 
{

NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];

NSDateComponents *componentsForReferenceDate = [calendar components:(NSCalendarUnitDay | NSCalendarUnitYear | NSCalendarUnitMonth ) fromDate:[NSDate date]];

[componentsForReferenceDate setDay:9];
[componentsForReferenceDate setMonth:11];
[componentsForReferenceDate setYear:2012];

NSDate *referenceDate = [calendar dateFromComponents:componentsForReferenceDate];

// set components for time 7:00 a.m.

NSDateComponents *componentsForFireDate = [calendar components:(NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond ) fromDate: referenceDate];

[componentsForFireDate setHour:7];
[componentsForFireDate setMinute:0];
[componentsForFireDate setSecond:0];

NSDate *fireDateOfNotification = [calendar dateFromComponents:componentsForFireDate];

// Create the notification

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

notification.fireDate = fireDateOfNotification;
notification.timeZone = [NSTimeZone localTimeZone];
notification.alertBody = [NSString stringWithFormat: @"Good Morning! Have a great day!"];
notification.alertAction = @"go back";
notification.userInfo= @{@"information": [NSString stringWithFormat:@"Some information"]};
notification.repeatInterval= NSCalendarUnitDay;
notification.soundName = UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];

}  
Run Code Online (Sandbox Code Playgroud)

如果这有帮助,请竖起大拇指!:d