transistorsoft / capacitor-background-fetch

Periodic callbacks in the background for both IOS and Android
78 stars 9 forks source link

background-fetch clashes with native custom IOS background Tasks #17

Closed SchallerStefan closed 1 year ago

SchallerStefan commented 1 year ago

Your Environment

Actual Behavior

Upon putting our own taskId in "Permitted background task scheduler identifiers" and registering it in "didFinishLaunchingWithOptions" using BGTaskScheduler.shared.register it clashes with the "capacitor-background-fetch"-Plugin. (Error is "task launcher for task with com... has already been registered")

Steps to Reproduce

  1. Put custom taskId in "Permitted background task scheduler identifiers"
  2. Register it in Method "didFinishLaunchingWithOptions" using BGTaskScheduler.shared.register

Context

The plugin probably registers all strings in "Permitted background task scheduler identifiers" because of BackgroundFetch.scheduleTask. I would use scheduleTask if it was a simple Background-Fetch and not use Background-processing (because it needs the phone to be connected and has more restrictions than fetch). The Background-Fetch-Plugin asks periodically, but I would like to control when the Background-Task is submitted myself.

My questions concerning this are:

  1. Can I in some way bypass the Background-Fetch-Plugin and have complete native control ourselves (maybe try/catch the initialization of the plugin in "didFinishLaunchingWithOptions"?)
  2. Does the Background-Fetch (not scheduleTask) use the same implementation as described in "https://developer.apple.com/documentation/uikit/app_and_environment/scenes/preparing_your_ui_to_run_in_the_background/using_background_tasks_to_update_your_app" If so, can I simply submit the custom Task myself with "BGTaskScheduler.shared.submit(request)" (where request has the custom taskId) or will this result in a backgroundProcess all the same?
  3. If both options are not really good, can I simply configure the Background-Fetch-Plugin with a minimumFetchInterval of 2 weeks and it would not clash with the Background-geolocation-plugin (it would use the same taskId "com.transistorsoft.fetch" so Im worried, that the geolocation would then also only update every 2 weeks)?
christocracy commented 1 year ago

On IOS I tried using backgroundTasks: "https://developer.apple.com/documentation/uikit/app_and_environment/scenes/preparing_your_ui_to_run_in_the_background/using_background_tasks_to_update_your_app" I expected this to work independently from the background-fetch-plugin.

This is the same API implemented by background-fetch (including the .scheduleTask method).

There is no need to implement the iOS BGTaskScheduler API manually.

Everything about the iOS BGTaskScheduler API is implemented by background-fetch.

SchallerStefan commented 1 year ago

If I configure the background-fetch to be 2 a weeks-interval, will it interfere with the Background-Fetch used by the Background-Geolocation-Plugin? Are both instances independent?

christocracy commented 1 year ago

An app is only allowed to register one iOS "background refresh" task.

There is only one instance of the BackgroundFetch instance. It is shared by both background-fetch and background-geolocation.

SchallerStefan commented 1 year ago

Thank you for the quick responses! Last Question: Why does .scheduleTask behave differently to the normal fetch Task of the plugin? (Mentioned in "https://transistorsoft.github.io/capacitor-background-fetch/classes/backgroundfetch.html" e.g. "scheduleTask seems only to fire when the device is plugged into power")

Is that because scheduleTask is a BGProcessingTask instead of a BGAppRefreshTask?

christocracy commented 1 year ago

Is that because scheduleTask is a BGProcessingTask instead of a BGAppRefreshTask?

Yes. It doesn't matter if you implement BGProcessingTask with your own native code vs using background-fetch.

iOS simply doesn't seem to run BGProcessingTask unless the device is plugged-in to power. Apple may change this behaviour with future iOS updates. That's up to Apple.

SchallerStefan commented 1 year ago

Ok, thank you again!