application:didReceiveRemoteNotification:fetchCompletionHandler Not Called
Asked Answered
R

8

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?

Rodolphe answered 17/9, 2013 at 17:29 Comment(2)
You might want to check your devices settings for background fetching go to Settings->General->Bacgkround App Refresh and make sure the appropriate settings are activated.Inspector
possible duplicate of iOS 7 Background Fetch - Will iOS launch my app into the background if it was force-quit by the user?Wilmot
N
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?

Nickell answered 14/2, 2014 at 20:48 Comment(6)
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?Inspector
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 switcherAware
@nvrtd, by "iOS 7 only" do you mean excluding iOS 8?All
@VanDuTran Good point. I haven't checked. Has anyone checked this on iOS 8? I assume they haven't changed it but I'm not sureNickell
I just found this issue on iOS 8.4.Cowbind
application:didReceiveRemoteNotification:fetchCompletionHandler: is needed for iOS 9.Anti
D
25
- (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;
}
Denisdenise answered 24/2, 2014 at 7:0 Comment(3)
-1, it's not process under force-quit. Code above runs when user launch application by tapping notification button.Mackay
great answer really this answer should be accepted.Aerobe
This really solves the problem! In application:didReceiveRemoteNotification: we get that notification "as usual".Harriott
B
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?

Borate answered 25/9, 2013 at 23:5 Comment(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 ReferenceOnassis
W
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
}
Watters answered 16/1, 2014 at 16:22 Comment(0)
C
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.

Calves answered 18/9, 2013 at 4:48 Comment(5)
"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?Roll
if the app is in "Not Running" state. remote notification never wake up the app.Calves
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.Nickell
@Calves 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.Nickell
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.Nickell
S
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.

Shelbashelbi answered 19/12, 2013 at 10:7 Comment(0)
E
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])
}
Extraordinary answered 16/10, 2015 at 8:40 Comment(0)
F
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?

Faultfinding answered 7/2, 2018 at 14:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.