BGTaskScheduler in Notifications Service extension

I am trying to schedule a background task using the BGTaskScheduler in the Notification Service extension but am receiving an error while trying to submit it:

(NSError) $R4 = 0x0000000157906d30 domain: "BGTaskSchedulerErrorDomain" - code: 3


Code for the task submission is pretty basic:

override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
    self.contentHandler = contentHandler
   
    let request = BGAppRefreshTaskRequest(identifier: "task")
    do {
        try BGTaskScheduler.shared.submit(request)
    } catch {
        NSLog("Could not schedule task \(error)")
    }
}


Documentation states that scheduling a task from the extension is possible but no additional info is provided: https://developer.apple.com/documentation/backgroundtasks/bgtaskscheduler?language=objc

Maybe I am missing something?

Accepted Reply

In the

BGTaskSchedulerErrorDomain
, code 3 is
BGTaskSchedulerErrorCodeNotPermitted
, which has an description of:

The task request could not be submitted because the appropriate background mode is not included in the

UIBackgroundModes
array, or its identifier was not present in the
BGTaskSchedulerPermittedIdentifiers
array in the app's
Info.plist
.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Replies

In the

BGTaskSchedulerErrorDomain
, code 3 is
BGTaskSchedulerErrorCodeNotPermitted
, which has an description of:

The task request could not be submitted because the appropriate background mode is not included in the

UIBackgroundModes
array, or its identifier was not present in the
BGTaskSchedulerPermittedIdentifiers
array in the app's
Info.plist
.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

I'm trying to implement a refresh backgroundtask that is scheduled when receive data in Notification Service Extension

override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
     DispatchQueue.main.async {
            self.cancelAllPendingBGTask()
            let request = BGAppRefreshTaskRequest(identifier: “com.hanbiro.task.gps”)
            // Fetch no earlier than 0.5 minutes from now
            request.earliestBeginDate = nil
            do {
                try BGTaskScheduler.shared.submit(request)
            } catch {
                print("Could not schedule app refresh: \(error)")}
       }

} 
      }
func cancelAllPendingBGTask() {
        BGTaskScheduler.shared.cancelAllTaskRequests()
    }


I already updated UIBackgroundModes, BGTaskSchedulerPermittedIdentifiers. But when debug on

try BGTaskScheduler.shared.submit(request)

by

e -l objc -- (void)[[BGTaskScheduler sharedScheduler] _simulateLaunchForTaskWithIdentifier:@"com.hanbiro.task.gps"]


my handler method on main app does not run, and debugger prints messages:

(lldb) e -l objc -- (void)[[BGTaskScheduler sharedScheduler] _simulateLaunchForTaskWithIdentifier:@"com.hanbiro.task.gps"]
2019-12-16 15:27:57.205297+0700 PushAlarmNotificationService[7785:990824] Simulating launch for task with identifier com.hanbiro.task.gps
2019-12-16 15:28:02.683175+0700 PushAlarmNotificationService[7785:991061] No task request with identifier com.haniro.task.gps has been scheduled

So have to put my handler method on extension if I want to schedule a task on extension? I want to put it (handler) on main app, is that OK?

Did you ever figure out what was causing this problem? I'm trying to run a BGProcessingTask on a device and I am submitting the command to test it but I get the error:


No task request with identifier com.***.yyyy has been scheduled


I have added the identifier to the info.plist.


Thanks for any help you might be able to give me!