Local Notification “Everyday at 7:00am” not notify

2019-02-11 08:37发布

问题:

I want a notification to go off everyday at 7:00, but it will not go off. I also want it to show in lock screen. Here is all the code I have so far.

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

回答1:

Try to use this:

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

}  

Thumbs up if this helped! :D



回答2:

You need to call

[[UIApplication sharedApplication] scheduleLocalNotiication:localNotification];

before you release localNotification. Your code does all the setup but doesn't actually schedule it, which is why you never get it.



回答3:

even though this is a very old question i just came here from google and yeah isn't it funny how everyone answers by just copying other peoples answer?

So for all the people like me coming from google, have a laugh at the spelling mistake that mite cost you a minute or two of wondering why it doesn't work.

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

works, but

[[UIApplication sharedApplication] scheduleLocalNotiication:localNotification];

Notiication doesn't...



回答4:

it's working

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        // Override point for customization after application launch.

        NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];

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

        [[UIApplication sharedApplication] cancelAllLocalNotifications];

        [componentsForReferenceDate setDay:27];
        [componentsForReferenceDate setMonth:10];
        [componentsForReferenceDate setYear:2016];

        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:8];
        [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: @"БЛАГОСЛОВИТЕ ПРОРОКА (с.а.в)"];
        notification.repeatInterval= NSCalendarUnitDay;
        notification.soundName = UILocalNotificationDefaultSoundName;
        [[UIApplication sharedApplication] setApplicationIconBadgeNumber: 1];
        [[UIApplication sharedApplication] scheduleLocalNotification:notification];
        [[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];

        return YES;

    }