diff --git a/NextcloudTalk/AppDelegate.m b/NextcloudTalk/AppDelegate.m index cdd85b67d..8ff2e0c99 100644 --- a/NextcloudTalk/AppDelegate.m +++ b/NextcloudTalk/AppDelegate.m @@ -85,6 +85,7 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:( [NCRoomsManager sharedInstance]; [self registerBackgroundFetchTask]; + [self registerBackgroundProcessingTask]; [NCUserInterfaceController sharedInstance].mainViewController = (NCSplitViewController *) self.window.rootViewController; [NCUserInterfaceController sharedInstance].roomsTableViewController = [NCUserInterfaceController sharedInstance].mainViewController.viewControllers.firstObject.childViewControllers.firstObject; @@ -141,6 +142,7 @@ - (void)applicationDidEnterBackground:(UIApplication *)application [self keepExternalSignalingConnectionAliveTemporarily]; [self scheduleAppRefresh]; + [self scheduleBackroundProcessing]; } @@ -354,6 +356,56 @@ - (NSString *)stringWithDeviceToken:(NSData *)deviceToken return [token copy]; } +#pragma mark - BackgroundProcessing + +- (void)registerBackgroundProcessingTask { + NSString *processingTaskIdentifier = [NSString stringWithFormat:@"%@.processing", NSBundle.mainBundle.bundleIdentifier]; + + // see: https://developer.apple.com/documentation/backgroundtasks/bgtaskscheduler?language=objc + [[BGTaskScheduler sharedScheduler] registerForTaskWithIdentifier:processingTaskIdentifier + usingQueue:nil + launchHandler:^(__kindof BGTask * _Nonnull task) { + [self handleBackgroundProcessing:task]; + }]; +} + +- (void)scheduleBackroundProcessing +{ + NSString *processingTaskIdentifier = [NSString stringWithFormat:@"%@.processing", NSBundle.mainBundle.bundleIdentifier]; + + BGProcessingTaskRequest *request = [[BGProcessingTaskRequest alloc] initWithIdentifier:processingTaskIdentifier]; + request.earliestBeginDate = [NSDate dateWithTimeIntervalSinceNow:UIApplicationBackgroundFetchIntervalMinimum]; + request.requiresNetworkConnectivity = YES; + request.requiresExternalPower = NO; + + NSError *error = nil; + [[BGTaskScheduler sharedScheduler] submitTaskRequest:request error:&error]; + + if (error) { + NSLog(@"Failed to submit background processing request: %@", error); + } +} + +- (void)handleBackgroundProcessing:(BGTask *)task +{ + [NCUtils log:@"Performing background processing -> handleBackgroundProcessing"]; + + // With BGTasks (iOS >= 13) we need to schedule another task when running in background + [self scheduleBackroundProcessing]; + + BGTaskHelper *bgTask = [BGTaskHelper startBackgroundTaskWithName:@"NCBackgroundProcessing" expirationHandler:^(BGTaskHelper *task) { + [NCUtils log:@"ExpirationHandler NCBackgroundProcessing called"]; + }]; + + // Check if the shown notifications are still available on the server + [[NCNotificationController sharedInstance] checkNotificationExistanceWithCompletionBlock:^(NSError *error) { + [NCUtils log:@"CompletionHandler checkNotificationExistance"]; + + [task setTaskCompletedWithSuccess:YES]; + [bgTask stopBackgroundTask]; + }]; +} + #pragma mark - BackgroundFetch / AppRefresh - (void)registerBackgroundFetchTask { diff --git a/NextcloudTalk/Info.plist b/NextcloudTalk/Info.plist index 8bea4dc0a..918fce371 100644 --- a/NextcloudTalk/Info.plist +++ b/NextcloudTalk/Info.plist @@ -6,6 +6,7 @@ $(PRODUCT_BUNDLE_IDENTIFIER) $(PRODUCT_BUNDLE_IDENTIFIER).refresh + $(PRODUCT_BUNDLE_IDENTIFIER).processing CFBundleDevelopmentRegion en