1

I would start a background task and give it an arguments which can be a json string. On Android we can use something like :

val oneOffTaskRequest = OneTimeWorkRequest.Builder(BackgroundWorker::class.java)
            .setInputData(buildTaskInputData(taskName, isInDebugMode, json))

How did that on IOS ?

let request = BGProcessingTaskRequest(
  identifier: SwiftWorkmanagerPlugin.defaultBGProcessingTaskIdentifier
)
BGTaskScheduler.shared.submit(request)

Request didn't have attributes which seems to help...

Eng
  • 1,617
  • 2
  • 12
  • 25
  • What is your `BGTaskRequest`? How do you construct it? What does it do? – Larme Jun 03 '22 at 09:29
  • Currently, your request does nothing you just registered it. You should look at Apple sample code https://developer.apple.com/documentation/backgroundtasks/refreshing_and_maintaining_your_app_using_background_tasks?language=objc You need the `BGTaskScheduler.shared.register(forTaskWithIdentifier:_:)` part – Larme Jun 03 '22 at 09:57
  • @Larme I already know that, I just didn't copy paste the 400 lines associated. I just wonder how to pass parameter to the background task process. – Eng Jun 03 '22 at 10:00
  • In `BGTaskScheduler.shared.register(forTaskWithIdentifier:_:)` you create at some point an object that need your JSON (I guess a HTTP request), so that's where you put the parameter. – Larme Jun 03 '22 at 10:02
  • @Larme Ok i'm looking to find how achieve that. This is actualy the code base, forked from the package workmanager. https://github.com/EA-YOUHOU/flutter_workmanager/tree/bb6af7d342e122807c3909528760cc7eaf670a53/ios/Classes – Eng Jun 03 '22 at 10:14
  • @Larme i didn't achieve it, i just think it's not possible... Do you have a tips ? – Eng Jun 03 '22 at 11:36
  • I don't use flutter, but I guess subclass `BackgroundWorker` to set your parameter there... – Larme Jun 03 '22 at 12:42
  • @Larme This is not really flutter related, the subject here is around BGTaskScheduler and argument... Same issue for ReactNative or Swift native developpers. – Eng Jun 03 '22 at 13:08
  • You don't understand. You submit the task. Then, at some point, the closure of `register(forTaskWithIdentifier:using:launchHandler:)` is called and that's where you create your own background "real code" task. There, you put whatever parameter you want. You can just write : `let urlRequest = URLRequest(...), request.httpBody = someJSONParameter; myURLSession.dataTask...` – Larme Jun 03 '22 at 13:13
  • 1
    Funnily enough I'm creating a kmm project that uses WorkManager on Android and obviously the equivilent offered on iOS is BGTaskScheduler. The 2 have the same fundemental goals, but the implementations are not very alike. You cannot submit a BGProcessingTaskRequest with any parameters, like a task id or a task specific download url, that can later be extracted as task parameters from the BGProcessingTask object in the closure. You are better off persisting any parameters when you submit a task and simply retrieve them from disk is the closure where you are provided the BGProcessingTask ... – Mark Aug 05 '22 at 16:05

0 Answers0