简体   繁体   中英

application:didReceiveRemoteNotification:fetchCompletionHandler Not Called

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?

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 (ie 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 (ie 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?

- (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;
}

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?

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
}

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.

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.

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])
}

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM