Sunwoo Park
Sunwoo Park

Reputation: 386

Executing background task upon receiving Remote Notifications(Silent Push) on iOS 7

I am currently building an app that utilizes iOS 7's Silent Push Notifications to wake the app in background on request.

As I would do in -applicationDidEnterBackground: I initiated a background task in -application:didReceiveRemoteNotification:fetchCompletionHandler: which looks like this:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler

{

    self.taskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{

        [[UIApplication sharedApplication] endBackgroundTask:self.taskId];

        self.taskId = UIBackgroundTaskInvalid;



        completionHandler(UIBackgroundFetchResultNewData);

    }];

}

While the app successfully registers for remote notifications and -application:didReceiveRemoteNotification:fetchCompletionHandler: is called on notification arrival, the app stays in background for just few seconds before returning to suspended state, much shorter than expected execution time given (when called in -applicationDidEnterBackground:) which I believe is around 3 minutes.

Is this an expected behaviour? Or is it not possible using remote notifications?

Upvotes: 1

Views: 1125

Answers (1)

SwiftArchitect
SwiftArchitect

Reputation: 48514

Have you properly configured the plist?

  1. You need to tell the iOS that you are an remote-notification App, and that you are requesting extra cycles when suspended.
  2. The UIBackgroundModes is subject to approval

From the documentation:

Background modes for apps: UIBackgroundModes value = remote-notification

The app wants to start downloading content when a push notification arrives. Use this notification to minimize the delay in showing content related to the push notification.

Upvotes: 0

Related Questions