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

前端 未结 4 492
暗喜
暗喜 2021-01-03 16:20

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)         


        
4条回答
  •  别那么骄傲
    2021-01-03 17:06

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

提交回复
热议问题