BGProcessingTaskRequest not triggered

Hi, I'm trying to test the new BackgroundTasks framework.

* Xcode 11.0 beta 3

* On the simulator, the submit task raises an error

* On my iPhone 7 beta 2, task doesn't be triggered


Here is the steps :

- I checked the "Background Processing" check box in capabilities

- I added com.example.refresh in the Info.plist

<key>BGTaskSchedulerPermittedIdentifiers</key>
  <array>
  <string>com.example.refresh</string>
  </array>


- I put the following code in the application didFinishLaunchingWithOptions :


// Background tasks testing
    if (@available(iOS 13.0, *)) {
        [[BGTaskScheduler sharedScheduler] registerForTaskWithIdentifier:@"com.example.refresh" usingQueue:nil launchHandler:^(__kindof BGTask * _Nonnull task) {
            UILocalNotification *notification = [[UILocalNotification alloc] init];
            notification.fireDate = [NSDate date];
            notification.alertBody = @"YEAH";
            [[UIApplication sharedApplication] presentLocalNotificationNow:notification];
            [task setTaskCompletedWithSuccess:YES];
        }];
        [[BGTaskScheduler sharedScheduler] cancelAllTaskRequests];
        BGProcessingTaskRequest *bgTaskRequest = [[BGProcessingTaskRequest alloc] initWithIdentifier:@"com.example.refresh"];
        NSDate *d = [NSDate date];
        int nbMin = 5;
        [bgTaskRequest setEarliestBeginDate:[d dateByAddingTimeInterval:(nbMin*60)]];
        [bgTaskRequest setRequiresNetworkConnectivity:NO];
        [bgTaskRequest setRequiresExternalPower:NO];
        NSError *e;
        [[BGTaskScheduler sharedScheduler] submitTaskRequest:bgTaskRequest error:&e];
        NSLog(@"ERROR TASK : %@", e);
        NSLog(@"Task planified at %@", [d dateByAddingTimeInterval:(nbMin*60)]);
    } else {
        // Fallback on earlier versions
    }


The notification is never triggered.

Replies

Nobody helps ?

Have you suspended your application? I believe background tasks won't trigger while application is running.

Also it would be usefull to look at the device console logs and see if there is anything happening at the time when you expect your task to execute

Yes, app is in inactive state.

Breaking News : Works now on latest beta (beta 4) 🙂

I am using Xcode 12.2 and using BGProcessTask to some task in the background. In iOS Device it works fine as i run the command to schedule the BGTask after the App goes to background, so in debug mode when i connect device to Mac and run commands then it works. Without running command When we background app and open the app the next day still the BGTask is not triggered. Can anyone has any suggestion on this?
Add a Comment