ionic-team / capacitor-background-runner

Other
33 stars 17 forks source link

Events can be called but are not scheduled automatically #50

Closed scriptPilot closed 8 months ago

scriptPilot commented 8 months ago

First of all thanks for the useful plugin!

I tried several hours with the existing documentation but ended up with the following situation:

Device: iPhone 11 Pro / iOS 16.6

capacitor.config.js

const config = { plugins: {}, ... }
...
config.plugins.BackgroundRunner = {
  label: 'de.domain.appname.background',
  src: 'background.js',
  event: 'myCustomEvent',
  repeat: true,
  interval: 1,
  autoStart: true
}
...
module.exports = config

background.js

addEventListener('myCustomEvent', (resolve, reject) => {
  console.log('myCustomEvent called')
  try {
    let scheduleDate = new Date();
    scheduleDate.setSeconds(scheduleDate.getSeconds() + 5);

    CapacitorNotifications.schedule([
      {
        id: 100,
        title: "Enterprise Background Runner",
        body: "A test message from the Enterprise Background Runner",
        scheduleAt: scheduleDate,
      },
    ]);

    resolve();
  } catch (err) {
    console.error(err);
    reject(err);
  }
});

App.vue (manuel dispatch)

BackgroundRunner.dispatchEvent({ label: 'de.domain.appname.background', event: 'myCustomEvent', details: {} })

What can I do as a next step for troubleshooting?

theproducer commented 8 months ago

Thanks!

What is the expectation here? Are you expecting to see myCustomEvent get called every 1 minute once the app is in the background? If so, that's not likely going to happen: https://capacitorjs.com/docs/apis/background-runner#limitations-of-background-tasks

While you can set an interval to define when your task runs after the app is backgrounded, or how often it should run, this is not guaranteed. iOS will determine when and how often you task will ultimately run, determined in part by how often you app is used.

scriptPilot commented 8 months ago

What can be expected from experience? Currently - after a couple of minutes - I do not even get one execution (log or local notification).

scriptPilot commented 8 months ago

In fact, 20 minutes later I got a first local notification. So it seems to work somehow but far away from any reliable schedule. But should be fine for many use cases :-)