46

I have something fairly simple I want to do. I attach a custom piece of data to some push notifications that I handle in

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

I look for the UIApplicationLaunchOptionsRemoteNotificationKey and hey presto there it is.

That method only gets called if my app is being launched for the first time. How do I read that same key if my application is running in the background already when the notification comes in and the user pressed the 'View' button on the notification? I want to send them to a particular view controller with that data open on it, the same as I do if the app is being launched for the first time from the notification.

2 Answers 2

107

Check out application:didReceiveRemoteNotification:fetchCompletionHandler: in iOS 7 and later.


The method application:didReceiveRemoteNotification: is called if your app is running in the foreground. It also is called if your app is running in the background and the user engages with your push notification (thus making your app active).

So, the real question is how to determine if the app was in the foreground or if it was made active by the user engaging with your push notification.

It looks like this answer to the question didReceiveRemoteNotification when in background has the key:

You can tell whether your app was just brought to the foreground or not in application:didReceiveRemoteNotification: using this bit of code:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    if ( application.applicationState == UIApplicationStateActive )
        // app was already in the foreground
    else
        // app was just brought from background to foreground
    ...
}
5
  • 13
    It's not really accurate to say the method ` application:didReceiveRemoteNotification` gets called if the app is running in the background. More accurately: If the app is backgrounded, the method gets called only after the user engages with the push notification to bring it to the foreground. If the user cancels / dismisses the push notification, the delegate method won't get called. Feb 6, 2013 at 23:17
  • 2
    As didRecieveRemoteNotification gets called when the device gets a notification (unlocked), and when the user clicks the notification view, it becomes hard (to my knowledge) to figure out if the user actually clicked the notification or the app just receive it. And checking the application state tells you nothing except its state, not if the user clicked the notification (in my experience).
    – Hrafn
    Feb 12, 2013 at 15:39
  • 4
    In iOS 7, with the remote-notification background mode, I don't think this technique works anymore, because the app can handle a remote notification in the background without bringing it to the foreground. So then how do you tell if it's brought from the background to the foreground?
    – user102008
    Oct 25, 2013 at 23:09
  • 1
    In iOS 7, with the remote-notification background mode, if I kill my app and it is no more in background and push notification comes, I can see banner as well as badge but I do not get callback application:didReceiveRemoteNotification nor application:didReceiveRemoteNotification:fetchCompletionHandler methods. When I tap on app icon, application:didFinishLaunchingWithOptions callback gets called but I don't get playload from [notification userInfo]. May 30, 2014 at 7:14
  • 1
    As Apple recommends, implement the application:didReceiveRemoteNotification:fetchCompletionHandler: method instead of this one whenever possible on iOS7 and above.
    – Allen
    Mar 11, 2015 at 7:38
-5

To detect if the app was activated by remote notication, try this:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    if (userInfo == NULL)
    {
        NSLog(@"didFinishLaunchingWithOptions user startup userinfo: %@", userInfo);
    }
    else
    {
        NSLog(@"didFinishLaunchingWithOptions notification startup userinfo: %@", userInfo);
    }
}
1
  • 1
    This doesn't account for the state where the app is launched, but in the background. Which is what the question is actually asking about. Aug 18, 2014 at 22:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.