JeeGee JeeGee - 2 months ago 8
Objective-C Question

App performs task in background (after receiving a remote notification), but finishes not completely

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


It is being called with
content-available = 1
. A download task starts (shows the
NSLogs
), but it does not complete the called task. Only when switching the app back to Foreground it finishes the previous called task.

Any idea... how this can happen?

Answer

you have to use UIBackgroundTaskIdentifier.to perform api call in back ground.

       - (void)applicationDidEnterBackground:(UIApplication *)application {




        isnotificationScreenopen = NO;
        UIBackgroundTaskIdentifier bgTaskId =[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
            [[UIApplication sharedApplication] endBackgroundTask:bgTaskId];
            NSLog(@"%f",[UIApplication sharedApplication].backgroundTimeRemaining);
        }];
        NSLog(@"%f",[UIApplication sharedApplication].backgroundTimeRemaining);



}

you can also read this link also

objective c - Proper use of beginBackgroundTaskWithExpirationHandler