title: Handling Push Notifications when App is NOT running
body: <p>When my App is <strong><em>not</em></strong> running and receives a Push Notification, if I click on that notification, the App is launched - but then it doesn't prompt the user with the Alert-View I set up, asking them whether they want to view the Notification's contents or not. It just launches, and sits there.</p>
  
  <p>The Push Notifications do work <em>perfectly</em> when the App <strong><em>is</em></strong> running - either as the Active app or while in the background - but nothing works correctly when the app is <strong><em>not</em></strong> running.</p>
  
  <p>I tried  logging-out the <code>launchOptions</code> NSDictionary in <code>application: didFinishLaunchingWithOptions:</code> to see what load its bringing - but it comes up as "(null)". So It basically contains nothing - which doesn't make sense cause shouldn't it contain the Notification's load?</p>
  
  <p>Anybody have any ideas how to make Push Notifications work when they arrive while the App was NOT running?</p>
  
  <p>EDIT: here's the code I'm using in <code>application: didReceiveRemoteNotification</code> just to see what's what:</p>
  
  <pre><code>if (UIApplicationStateBackground) {
  
      NSLog(@"===========================");
      NSLog(@"App was in BACKGROUND...");
  }
  else if (UIApplicationStateActive == TRUE) {
      NSLog(@"===========================");
      NSLog(@"App was ACTIVE");
  }
  else {
      [[UIApplication sharedApplication] setApplicationIconBadgeNumber: 99]; 
      UIAlertView *BOOM = [[UIAlertView alloc] initWithTitle:@"BOOM"
                                                     message:@"app was INACTIVE"
                                                    delegate:self
                                           cancelButtonTitle:@"a-ha!"
                                           otherButtonTitles:nil];
      [BOOM show];
      NSLog(@"App was NOT ACTIVE");
  }
  </code></pre>
  
  <p>So this is supposed to take care of all the application's states - but its not. Push Notifications are only working when the app is running - either in the background or in the foreground...</p>
  
  <p>================================================</p>
  
  <p>UPDATE/EDIT#2:
  as per "@dianz" suggestion (below,) I modified the code of my <code>application: didFinishLaunchingWithOptions</code> to include the following:</p>
  
  <pre><code>UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
  if (localNotif) {
      NSString *json = [localNotif valueForKey:@"data"]; 
  
      UIAlertView *bam = [[UIAlertView alloc] initWithTitle:@"appDidFinishWithOptions"
                                                    message:json   
                                                   delegate:self
                                          cancelButtonTitle:@"cool"
                                          otherButtonTitles:nil];
      [bam show];
  
  }
  </code></pre>
  
  <p>This does make the AlertView box appear, but there seems to be no payload: the title of the AlertView shows up ("appDidFinishWithOptions"), but the json NSString comes up EMPTY... Will keep tweaking...</p>
  
  <p>======================</p>
  
  <p>EDIT #3 - its now working <em>almost</em> 100% <br/>
  So, in <code>didFinishLaunchingWithOptions</code>: </p>
  
  <pre><code>UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
      if (localNotif) {
          // Grab the pushKey:
          pushKey = [localNotif valueForKey:@"pushKey"];
          // "pushKey" is an NSString declared globally
          // The "pushKey" in the "valueForKey" statement is part of my custom JSON Push Notification pay-load.
          // The value of pushKey will always be a String, which will be the name of the 
          // screen I want the App to navigate to. So when a Notification arrives, I go
          // through an IF statement and say "if pushKey='specials' then push the                                      
          // specialsViewController on, etc.
  
          // Here, I parse the "alert" portion of my JSON Push-Notification:
          NSDictionary *tempDict = [localNotif valueForKey:@"aps"];
          NSString *pushMessage = [tempDict valueForKey:@"alert"];
  
  
          // Finally, ask user if they wanna see the Push Notification or Cancel it:
          UIAlertView *bam = [[UIAlertView alloc] initWithTitle:@"(from appDidFinishWithOptions)"
                                                        message:pushMessage  
                                                       delegate:self
                                              cancelButtonTitle:@"Cancel"
                                              otherButtonTitles:@"View Info", nil];
          [bam show];
  
      }
  </code></pre>
  
  <p>I next implement the <code>alertView: clickedButtonAtIndex</code> method to see what the user chose on the AlertView and proceed accordingly.</p>
  
  <p>This, along with the <code>didReceiveRemoteNotification</code> logic works perfectly.</p>
  
  <p>HOWEVER... when the app is NOT running, and I send it a Push Notification, if I DON'T click on the Push Notification alert as soon as it arrives and instead wait for it to fade out (which happens after like 3-4 seconds), and <em>then</em> I click on the App's icon - which now has a BADGE of 1 on it - the app launches, but I don't get the Push Notification alert at all when it launches. It just sits there.</p>
  
  <p>Guess I need to figure <em>that</em> permutation next...</p>