How to respond to push notification view if app is already running in the background
Asked Answered
B

2

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.

Batavia answered 24/2, 2011 at 1:45 Comment(0)
D
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
    ...
}
Dendrology answered 24/3, 2011 at 19:28 Comment(5)
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.Openeyed
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).Wuhan
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?Stebbins
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].Pop
As Apple recommends, implement the application:didReceiveRemoteNotification:fetchCompletionHandler: method instead of this one whenever possible on iOS7 and above.Carson
O
-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);
    }
}
Overplus answered 31/7, 2014 at 15:47 Comment(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.Iorio

© 2022 - 2024 — McMap. All rights reserved.