title: How to reschedule a local notification when a local notification is received?
body: <p>I am writing an hourly timer app in objective-c.  The app will schedule a local notification to fire optionally at quarter past, half past the hour, and on the hour.  The code calculates the firedate correctly and the local notification comes in at the right time. The code for rescheduling the next notification is also working whether the app is in the foreground or the background, <strong>but not when the phone is locked</strong>. I have not found a way to reschedule a notification for the next interval (quarter hour, half hour, or hour) without receiving a local notification. This means that the user has to receive an alert and then tap on the alert to get the <strong>didReceiveLocalNotification</strong> method in the app delegate to run. So the crux of the problem is <strong>getting a local notification to wake the app delegate when the phone is locked</strong>. The <strong>launchOption objectForKey</strong> method is not called if I set a breakpoint there. Is this possible? Note: this is not a question of using a repeated notification. Here is the app delegate code:</p>
  
  <pre><code>    //
  //  AppDelegate.m
  //  Hourly Chime2
  //
  //  Created by Nelson Capes on 9/20/16.
  //  Copyright © 2016 Nelson Capes. All rights reserved.
  //
  
  #import "AppDelegate.h"
  #import &lt;AVFoundation/AVFoundation.h&gt;
  #import "HourlyChimeTableViewController.h"
  @interface AppDelegate ()
  
  @end
  
  @implementation AppDelegate
  
  
  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
      // Override point for customization after application launch.
  
      NSError *sessionError = nil;
      NSError *activationError = nil;
      [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&amp;sessionError];
      [[AVAudioSession sharedInstance] setActive: YES error: &amp;activationError];
  
      if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]){
  
          [application registerUserNotificationSettings:[UIUserNotificationSettings
                                                         settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|
                                                         UIUserNotificationTypeSound categories:nil]];
      }
      UILocalNotification *localNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
      if (localNotification) {
          // Set icon badge number to zero
          application.applicationIconBadgeNumber = 0;
          HourlyChimeTableViewController *controller = [[HourlyChimeTableViewController alloc]init];
          [controller stopTimer];
          [controller startTimer];
          NSUserDefaults *storage = [NSUserDefaults standardUserDefaults];
          NSInteger interval = [storage integerForKey:@"intervalToWatch"];
          [controller setExpirationTime:interval];
  
      }
  
      return YES;
  }
  
  
  - (void)applicationWillResignActive:(UIApplication *)application {
      // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
      // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
  }
  - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
  {
      UIApplicationState state = [application applicationState];
      if (state == UIApplicationStateActive) {
          UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Reminder"
                                                          message:notification.alertBody
                                                         delegate:self cancelButtonTitle:@"OK"
                                                otherButtonTitles:nil];
          [alert show];
      }
      HourlyChimeTableViewController *controller = [[HourlyChimeTableViewController alloc]init];
      [controller stopTimer];
      [controller startTimer];
      NSUserDefaults *storage = [NSUserDefaults standardUserDefaults];
      NSInteger interval = [storage integerForKey:@"intervalToWatch"];
      [controller setExpirationTime:interval];
  
      // Set icon badge number to zero
      application.applicationIconBadgeNumber = 0;
  }
  
  - (void)applicationDidEnterBackground:(UIApplication *)application {
      // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
      // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
      HourlyChimeTableViewController *controller = [[HourlyChimeTableViewController alloc]init];
      [controller stopTimer];
  }
  
  
  - (void)applicationWillEnterForeground:(UIApplication *)application {
      // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
  }
  
  
  - (void)applicationDidBecomeActive:(UIApplication *)application {
      // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
      HourlyChimeTableViewController *controller = [[HourlyChimeTableViewController alloc]init];
      [controller startTimer];
  
  }
  
  
  - (void)applicationWillTerminate:(UIApplication *)application {
      // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
  }
  
  
  @end
  </code></pre>
  
  <p>And here is the code that gets called when the local notification is received (note interval is set to 14 minutes just for testing - local notification is received one minute later:</p>
  
  <pre><code>    -(void)setExpirationTime:(NSInteger)interval{
  
      NSDate *today = [[NSDate alloc] init];
      NSCalendar *calendar = [NSCalendar currentCalendar];
      NSDateComponents *components= [calendar components:(NSCalendarUnitMinute) | (NSCalendarUnitSecond) fromDate: today];
      NSInteger minute = [components minute];
      NSInteger second = [components second];
      NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
  
      switch(interval){
          case 0:{
              NSInteger difference;
              minute =14;
             // NSInteger difference = 10;
              [offsetComponents setHour:0];
              [offsetComponents setMinute:0];
              if(minute &gt;= 0 &amp;&amp; minute &lt; 15){
                  difference = 900 - minute*60 - second;//seconds left to quarter past
              }else{
                  if(minute &gt;= 15 &amp;&amp; minute &lt; 30){
                      difference = 1800 - minute*60 - second;// seconds to half past
                  }else{
                      if (minute &gt;= 30 &amp;&amp; minute &lt; 45){// seconds to quarter to
                          difference = 2700 - minute*60 - second;
                      }else{// seconds to the hour
                          difference = 3600 - minute*60 - second;
                      }
                  }
              }
          //    difference = 60;
              [offsetComponents setSecond:difference];
              NSDate *fireDate = [calendar dateByAddingComponents:offsetComponents
                                                            toDate:today options:0];
              [self startLocalNotification:fireDate];
              break;
          }
          case 1:{
              NSInteger difference;
             // NSInteger difference = 60;
              [offsetComponents setHour:0];
              [offsetComponents setMinute:0];
              if(minute &gt;= 0 &amp;&amp; minute &lt; 30){
                  difference = 1800 - minute*60 - second;// seconds to half past
              }else{
                  difference = 3600 - minute*60 - second;// seconds to the hour
              }
              [offsetComponents setSecond:difference];
              NSDate *fireDate = [calendar dateByAddingComponents:offsetComponents
                                                            toDate:today options:0];
              [self startLocalNotification:fireDate];
              break;
          }
          case 2:{
              NSInteger difference = 3600 - minute*60 - second;
           //   NSInteger difference = 60;
              [offsetComponents setHour:0];
              [offsetComponents setMinute:0];
              [offsetComponents setSecond:difference];
              NSDate *fireDate = [calendar dateByAddingComponents:offsetComponents
                                                            toDate:today options:0];
              [self startLocalNotification:fireDate];
              break;
          }
  
      }
  
  
  }
  -(void)startLocalNotification:(NSDate *)fireDate{
  
    //  [[UIApplication sharedApplication] cancelAllLocalNotifications];
      UILocalNotification *notification = [[UILocalNotification alloc]init];
      notification.fireDate = fireDate;
      notification.alertBody =@"Timer Expired!";
      notification.alertTitle = @"TimeChime Alert";
      notification.timeZone = [NSTimeZone defaultTimeZone];
      notification.soundName = UILocalNotificationDefaultSoundName;
     // notification.repeatInterval = NSCalendarUnitMinute;
  
      [[UIApplication sharedApplication]scheduleLocalNotification:notification];
  
  }
  </code></pre>