23

It appears the function application:didReceiveRemoteNotification:fetchCompletionHandler is not called when the app has been forcefully quit. It was my impression that the function would be invoked no matter what state the app was in, but it appears that it is only called if the app is already running in the background. Is there a way to wake up an app in the background if it is not already running using the new iOS 7 remote notification background mode?

2

8 Answers 8

52

application:didReceiveRemoteNotification:fetchCompletionHandler: gets called even if the app is suspended, not running at all, backgrounded, or active. Also worth noting that the method is iOS 7 only. Here is the apple documentation.

HOWEVER if the app was forcibly closed (i.e. by killing with the app switcher), the app will not be launched. (see SO answer) EDIT: I checked this again on iOS 7.1 to see if they fixed this, but it still remains the case that if the app is killed manually, app will NOT be woken up and application:didReceiveRemoteNotification:fetchCompletionHandler: will not be called

When receiving the push, the app is only woken up only "if needed" to call the application:didReceiveRemoteNotification:fetchCompletionHandler: method (i.e. you have to set the "content-available" flag within the push notification payload. See SO answer). The method will be called again if the user then opens the app by tapping the notification.

EDIT: haven't checked this on iOS 8. Has anyone else?

6
  • 4
    Though it seems confusing that it's said that the method will be called when the application is not running at all, though it won't run if the application has been forcibly closed. Is that another application state?
    – Biclops
    Feb 20, 2014 at 1:22
  • 6
    It is not another application state, iOS sees forcibly closing the app as the user not wishing to have ANY process thread run by that app until it is next launched. It is working as Apple intends, though their design reasoning is questionable if not altogether ass backwards and stupid with how people actually use the app switcher
    – Jeremy
    Mar 3, 2014 at 9:07
  • @nvrtd, by "iOS 7 only" do you mean excluding iOS 8? Apr 9, 2015 at 19:25
  • 1
    I just found this issue on iOS 8.4.
    – reallyseth
    Aug 19, 2015 at 14:47
  • 1
    application:didReceiveRemoteNotification:fetchCompletionHandler: is needed for iOS 9.
    – Michael
    Apr 13, 2016 at 20:00
24
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions {

    //Remote Notification Info
    NSDictionary * remoteNotifiInfo = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];

    //Accept push notification when app is not open
    if (remoteNotifiInfo) {
       [self application:application didReceiveRemoteNotification: remoteNotifiInfo];
    }

    return YES;
}
3
  • 2
    -1, it's not process under force-quit. Code above runs when user launch application by tapping notification button.
    – dimpiax
    Mar 23, 2015 at 0:20
  • 1
    great answer really this answer should be accepted. Jun 11, 2015 at 7:55
  • This really solves the problem! In application:didReceiveRemoteNotification: we get that notification "as usual". Sep 1, 2015 at 15:49
3

The app should be launched even if it is not running. The Apple documentation says:

When this value is present and a push notification arrives on a device, the system sends the notification to your app (launching it if needed) and gives it a few moments to process > the notification before displaying anything to the user. (iOS App Programming Guide)

When a push notification arrives, the system displays the notification to the user and launches the app in the background (if needed) so that it can call this method. (UIApplicationDelegate Protocol Reference)

Unlike the application:didReceiveRemoteNotification: method, which is called only when your app is running, the system calls this method regardless of the state of your app. If your app is suspended or not running, the system wakes up or launches your app and puts it into the background running state before calling the method. (UIApplicationDelegate Protocol Reference)

However when testing with "content-available":1 pushes the app is never launched when it is not running. When the app is suspended it works.

Did you find yourself a solution Wes?

1
  • @ emiel : I need your help for when my app is terminated / suspended . now i get one notification and send data to server without open app . how it is possible or which method call of UIApplicationDelegate Protocol Reference May 13, 2017 at 10:59
3

When your app has been force-quitted that method is not called. Instead, as usual, (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions is called.

If the app was opened by tapping "Open" in a notification-popup, the notification is inside launchOptions.

Get the push-notification dictionary like this:

NSDictionary * pushNotificationUserInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

if (pushNotificationUserInfo)
{
  // call your handler here
}
2

As documented by Apple, the new multitasking API (fetch and remote-notification) will work only when the app in the suspended/background/foreground state.

  • In the background/foreground state, then application:didReceiveRemoteNotification:fetchCompletionHandler will get triggered.

  • In the Suspended state, then -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions will get triggered.

  • In the Not Running state (your case), application:didReceiveRemoteNotification:fetchCompletionHandler never gets triggered.

Please refer to the Apple documentation for more about app states.

5
  • 6
    "Unlike the application:didReceiveRemoteNotification: method, which is called only when your app is running, the system calls this method regardless of the state of your app. If your app is suspended or not running, the system wakes up or launches your app and puts it into the background running state before calling the method.". are you sure?
    – lucaconlaq
    Sep 28, 2013 at 15:40
  • if the app is in "Not Running" state. remote notification never wake up the app.
    – Nandha
    Oct 7, 2013 at 11:48
  • 2
    I agree with Luka's comment. The answer is not correct. application:didReceiveRemoteNotification:fetchCompletionHandler: gets called even if the app is suspended, not running at all, or backgrounded. Also worth noting that the above method is iOS 7 only.
    – nvrtd frst
    Feb 11, 2014 at 9:26
  • 1
    @Nandha yes I have. Just to be clear, the method that is being asked about here is application:didReceiveRemoteNotification:fetchCompletionHandler: which is different from application:didReceiveRemoteNotification: Here is the apple documentation for the first method.
    – nvrtd frst
    Feb 14, 2014 at 20:10
  • 1
    Also worth noting: when receiving the push, the app is only woken up "if needed" to call the application:didReceiveRemoteNotification:fetchCompletionHandler: method. i.e. within the push notification payload, you have to set the "content-available" flag (see SO answer). The method will be called again if the user then opens the app by tapping the notification.
    – nvrtd frst
    Feb 14, 2014 at 20:39
1

If you force quit an app from the application switcher, it will not be woken in the background (via any means) until after the next time it is next launched. When you force quit an app, you are effectively saying to the OS that you do not want this app to run at all, even if a background event would normally have woken it.

This is worth watching out for during testing, as you may have force quit the app in order to check that it gets launched via the push notification when the app is not running. In fact, your use of force quit will be the reason why it doesn't get launched.

1

To whom concern in Swift 2.0 I've solved my problem like this is for the background

if let remoteNotification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? NSDictionary {

    pushNotificationAction(remoteNotification as [NSObject : AnyObject])
}
0

I recently met the same problem, and I found Apple updated their documentation and says:

However, the system does not automatically launch your app if the user has force-quit it. In that situation, the user must relaunch your app or restart the device before the system attempts to launch your app automatically again.

application(_:didReceiveRemoteNotification:fetchCompletionHandler:)

So I guess there's no way to do anything when the app is killed by force quit?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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