Skip to content

Commit 46a3076

Browse files
committed
Add background processing
Signed-off-by: Marcel Müller <[email protected]>
1 parent c0b1f80 commit 46a3076

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

NextcloudTalk/AppDelegate.m

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
8585
[NCRoomsManager sharedInstance];
8686

8787
[self registerBackgroundFetchTask];
88+
[self registerBackgroundProcessingTask];
8889

8990
[NCUserInterfaceController sharedInstance].mainViewController = (NCSplitViewController *) self.window.rootViewController;
9091
[NCUserInterfaceController sharedInstance].roomsTableViewController = [NCUserInterfaceController sharedInstance].mainViewController.viewControllers.firstObject.childViewControllers.firstObject;
@@ -141,6 +142,7 @@ - (void)applicationDidEnterBackground:(UIApplication *)application
141142

142143
[self keepExternalSignalingConnectionAliveTemporarily];
143144
[self scheduleAppRefresh];
145+
[self scheduleBackroundProcessing];
144146
}
145147

146148

@@ -354,6 +356,56 @@ - (NSString *)stringWithDeviceToken:(NSData *)deviceToken
354356
return [token copy];
355357
}
356358

359+
#pragma mark - BackgroundProcessing
360+
361+
- (void)registerBackgroundProcessingTask {
362+
NSString *processingTaskIdentifier = [NSString stringWithFormat:@"%@.processing", NSBundle.mainBundle.bundleIdentifier];
363+
364+
// see: https://developer.apple.com/documentation/backgroundtasks/bgtaskscheduler?language=objc
365+
[[BGTaskScheduler sharedScheduler] registerForTaskWithIdentifier:processingTaskIdentifier
366+
usingQueue:nil
367+
launchHandler:^(__kindof BGTask * _Nonnull task) {
368+
[self handleBackgroundProcessing:task];
369+
}];
370+
}
371+
372+
- (void)scheduleBackroundProcessing
373+
{
374+
NSString *processingTaskIdentifier = [NSString stringWithFormat:@"%@.processing", NSBundle.mainBundle.bundleIdentifier];
375+
376+
BGProcessingTaskRequest *request = [[BGProcessingTaskRequest alloc] initWithIdentifier:processingTaskIdentifier];
377+
request.earliestBeginDate = [NSDate dateWithTimeIntervalSinceNow:UIApplicationBackgroundFetchIntervalMinimum];
378+
request.requiresNetworkConnectivity = YES;
379+
request.requiresExternalPower = NO;
380+
381+
NSError *error = nil;
382+
[[BGTaskScheduler sharedScheduler] submitTaskRequest:request error:&error];
383+
384+
if (error) {
385+
NSLog(@"Failed to submit background processing request: %@", error);
386+
}
387+
}
388+
389+
- (void)handleBackgroundProcessing:(BGTask *)task
390+
{
391+
[NCUtils log:@"Performing background processing -> handleBackgroundProcessing"];
392+
393+
// With BGTasks (iOS >= 13) we need to schedule another task when running in background
394+
[self scheduleBackroundProcessing];
395+
396+
BGTaskHelper *bgTask = [BGTaskHelper startBackgroundTaskWithName:@"NCBackgroundProcessing" expirationHandler:^(BGTaskHelper *task) {
397+
[NCUtils log:@"ExpirationHandler NCBackgroundProcessing called"];
398+
}];
399+
400+
// Check if the shown notifications are still available on the server
401+
[[NCNotificationController sharedInstance] checkNotificationExistanceWithCompletionBlock:^(NSError *error) {
402+
[NCUtils log:@"CompletionHandler checkNotificationExistance"];
403+
404+
[task setTaskCompletedWithSuccess:YES];
405+
[bgTask stopBackgroundTask];
406+
}];
407+
}
408+
357409
#pragma mark - BackgroundFetch / AppRefresh
358410

359411
- (void)registerBackgroundFetchTask {

NextcloudTalk/Info.plist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<array>
77
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
88
<string>$(PRODUCT_BUNDLE_IDENTIFIER).refresh</string>
9+
<string>$(PRODUCT_BUNDLE_IDENTIFIER).processing</string>
910
</array>
1011
<key>CFBundleDevelopmentRegion</key>
1112
<string>en</string>

0 commit comments

Comments
 (0)