@@ -85,6 +85,7 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
85
85
[NCRoomsManager sharedInstance ];
86
86
87
87
[self registerBackgroundFetchTask ];
88
+ [self registerBackgroundProcessingTask ];
88
89
89
90
[NCUserInterfaceController sharedInstance ].mainViewController = (NCSplitViewController *) self.window .rootViewController ;
90
91
[NCUserInterfaceController sharedInstance ].roomsTableViewController = [NCUserInterfaceController sharedInstance ].mainViewController .viewControllers .firstObject .childViewControllers .firstObject ;
@@ -141,6 +142,7 @@ - (void)applicationDidEnterBackground:(UIApplication *)application
141
142
142
143
[self keepExternalSignalingConnectionAliveTemporarily ];
143
144
[self scheduleAppRefresh ];
145
+ [self scheduleBackroundProcessing ];
144
146
}
145
147
146
148
@@ -354,6 +356,56 @@ - (NSString *)stringWithDeviceToken:(NSData *)deviceToken
354
356
return [token copy ];
355
357
}
356
358
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
+
357
409
#pragma mark - BackgroundFetch / AppRefresh
358
410
359
411
- (void )registerBackgroundFetchTask {
0 commit comments