How to get local notification at two different timings daily?

40 views Asked by At

I want my app to send Local Notification at 7AM and 7PM daily. How to do that? please help

I need to send two local notification daily one at 7AM and another at 7PM.

{ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedOnce"])
    {
          static dispatch_once_t once;
          dispatch_once(&once, ^ {
            NSLog(@"Do it once");
            NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar] ;
            NSDate *now = [NSDate date];
            NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth |  NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:now];
            [components setHour:13];
            [components setMinute:1];

            UILocalNotification *notification = [[UILocalNotification alloc]init];
            notification.fireDate = [calendar dateFromComponents:components];
            notification.repeatInterval = kCFCalendarUnitDay;
            [notification setAlertBody:@"AM U got notification!!!"];
            // notification.soundName = UILocalNotificationDefaultSoundName;
            [[UIApplication sharedApplication] scheduleLocalNotification:notification];
            [components setHour:17];
            [components setMinute:28];
            notification.fireDate = [calendar dateFromComponents:components];
            notification.repeatInterval = NSCalendarUnitDay;
            [notification setAlertBody:@"PM U got notification!!!"];            
         // notification.soundName = UILocalNotificationDefaultSoundName;
            [[UIApplication sharedApplication] scheduleLocalNotification:notification];
        });
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedOnce"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }

I have written the if condition to run the code only once after installing the app or else every time I open the app local notification is getting set. Now for the same day it is working fine but for the next day it is not getting repeated. please help. or is there any other way to get daily local notification at 7AM and 7PM?

1

There are 1 answers

1
Darren On

There is no repeat on local notifications. You would have to schedule multiple notifications for however many days you want them to be scheduled for.