2 Replies
      Latest reply on Dec 16, 2019 12:34 AM by vanzk
      ohelei Level 1 Level 1 (0 points)

        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?

        • Re: BGTaskScheduler in Notifications Service extension
          eskimo Apple Staff Apple Staff (12,675 points)

          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"

            • Re: BGTaskScheduler in Notifications Service extension
              vanzk Level 1 Level 1 (0 points)

              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?