title: Handle UILocalNotification in UIViewController using NSNotification after the app has been terminated
body: <p>I'm working with <code>UILocalNotification</code> and I would like to notify one of my controller that the notification has been received even if the app has been terminated.</p>
  
  <p>In my appDelegate I implemented this function:</p>
  
  <pre><code>-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
  {
      if ([application applicationState] == UIApplicationStateInactive) {
          [[NSNotificationCenter defaultCenter] postNotificationName:@"localNotificationReceived" object:notification.userInfo];
      }
  }
  </code></pre>
  
  <p>In my <code>UIViewController</code> I implemented the observer on the <code>viewDidLoad</code> method</p>
  
  <pre><code>- (void)viewDidLoad
  {
      [super viewDidLoad];
  
      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didlocalNotificationReceived:) name:@"localNotificationReceived" object:nil];   
  
  }
  </code></pre>
  
  <p>It works perfectly when the app run in background. Since the <code>viewDidLoad</code> method has already been called and the Observer is waiting..</p>
  
  <p>The issue is when I kill the app. Then the observer of my controller is gone and the <code>didlocalNotificationReceived</code> method is never called.</p>
  
  <p>I think that it's because when I receive the <code>localNotification</code> and run the app again. The <code>didReceiveLocalNotification:</code> method is called before the <code>viewDidLoad</code> of my <code>UIViewController</code>. Then the observer is created after the <code>PostNotificationName</code> then the observer receives nothing.</p>
  
  <p>I would like to know if there is some best practices or pattern to handle this kind of issue.</p>
  
  <p>I know that the <code>didFinishLaunchingWithOptions</code> method is called before <code>didlocalNotificationReceived</code> so there is probably something to do there.</p>
  
  <p>UPDATE :</p>
  
  <p>I also discovered that when is app is terminated. Once you tap the notification, It opens the app, call the function <code>didFinishLaunchingWithOptions</code> but never call <code>didReceiveLocalNotification</code>. So I think that I'm gonna handle both cases differently.</p>