Live activity is not updating when using background tasks

2k Views Asked by At

I am trying to implement a live activity in my application and update it every 5 minutes using BackgroundTasks. However, the live activity does not update neither in the background nor in the foreground.

The steps that I did to implement the background tasks is as follows:

1- Added a new capability in the main target (Background Mode) and selected background fetch and background processing.

2- Registered the background task in the app delegate inside didFinishLaunchingWithOptions.

BGTaskScheduler.shared.register(forTaskWithIdentifier: "IDENTIFIER", using: nil) { task in
     LAManager.shared.handleAppRefresh(task: task as! BGProcessingTask)
}

3- Added the background task identifier in info.plist.

4- Implemented a method to schedule the task.

func scheduleAppRefresh() {
     let request = BGAppRefreshTaskRequest(identifier: "IDENTIFIER")

     // handle refresh based on the time added to now
     request.earliestBeginDate = Date(timeIntervalSinceNow: 5 * 60)

     do {
         try BGTaskScheduler.shared.submit(request)
     } catch {
         print("Could not schedule app refresh: \(error)")
     }
}

5 - Another method to handle the task (and schedule a new task).

func handleAppRefresh(task: BGProcessingTask) {
    scheduleAppRefresh()

    task.expirationHandler = {
       //DO Something after task expired
    }

    // do updates here
}

I am still not getting any updates to the live activity.

0

There are 0 best solutions below