应用程序在后台执行任务(在收到远程通知后),但未完全完成

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

正在用 content-available = 1 调用它。下载任务开始(显示 NSLogs),但它没有完成调用的任务。只有当应用程序切换回前台时,它才会完成之前调用的任务。

知道...这是怎么发生的吗?

您必须使用 UIBackgroundTaskIdentifier.to 在后台执行 api 调用。

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



}

你也可以阅读这个link也

objective c - Proper use of beginBackgroundTaskWithExpirationHandler