title: iOS push notification: how to detect if the user tapped on notification when the app is in background?
body: <p>There are a lot of stackoverflow threads regarding this topic, but I still didn't find a good solution.</p>
  
  <p>If the app is not in the background, I can check <code>launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]</code> in <code>application:didFinishLaunchingWithOptions:</code> call to see if it's opened from a notification.</p>
  
  <p>If the app is in the background, all the posts suggest to use <code>application:didReceiveRemoteNotification:</code> and check the application state. But as I experimented (and also as the name of this API suggests), this method gets called when the notification is received, instead of tapped.</p>
  
  <p>So the problem is, if the app is launched and then backgrounded, and you know a notification is received from <code>application:didReceiveNotification</code> (<code>application:didFinishLaunchWithOptions:</code> won't trigger at this point), how do you know if user resumed the app from by tapping the notification or just tapping the app icon? Because if the user tapped the notification, the expectation is to open the page mentioned in that notification. Otherwise it shouldn't.</p>
  
  <p>I could use <code>handleActionWithIdentifier</code> for custom action notifications, but this only gets triggered when a custom action button is tapped, not when the user taps on the main body of the notification.</p>
  
  <p>Thanks.</p>
  
  <p>EDIT:</p>
  
  <p>after reading one answer below, I thought in this way I can clarify my question:</p>
  
  <p>How can we differentiate these 2 scenarios:</p>
  
  <p>(A) 1.app goes to background; 2.notification received; 3. user taps on the notification; 4. app enters foreground</p>
  
  <p>(B) 1.app goes to background; 2.notification received; 3. user ignores the notification and taps on the app icon later; 4. app enters foreground</p>
  
  <p>Since <code>application:didReceiveRemoteNotification:</code> is triggered in both cases at step 2.</p>
  
  <p>Or, should <code>application:didReceiveRemoteNotification:</code> be triggered in step 3 for (A) only, but I somehow configured my app wrong so I'm seeing it at step 2?</p>