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.

rustyshelf
  • 44,963
  • 37
  • 98
  • 104

2 Answers2

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
    ...
}
Community
  • 1
  • 1
gerry3
  • 21,420
  • 9
  • 66
  • 74
  • 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. – Martijn Thé Feb 06 '13 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 '13 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 '13 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]. – Bharat Dodeja May 30 '14 at 07:14
  • 1
    As Apple recommends, implement the `application:didReceiveRemoteNotification:fetchCompletionHandler: ` method instead of this one whenever possible on iOS7 and above. – Allen Mar 11 '15 at 07: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);
    }
}
user523234
  • 14,323
  • 10
  • 62
  • 102
  • 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. – Jesse Naugher Aug 18 '14 at 22:14