transistorsoft / capacitor-background-fetch

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

The plugin is not fetching the points every 15 minutes if the device is in the same location. #21

Closed projul closed 1 year ago

projul commented 1 year ago

Your Environment

Plugin version: "@transistorsoft/capacitor-background-fetch": "^5.0.0"

Platform: iOS or Android Device OS version: any version

Device manufacturer / model: iOS and Android any device XCode version: Version 14.3 (14E222b)

Capacitor info (npx cap doctor) Latest Dependencies:

@capacitor/cli: 5.0.4 @capacitor/core: 5.0.4 @capacitor/android: 5.0.4 @capacitor/ios: 5.0.4

Installed Dependencies:

@capacitor/cli: 5.0.4 @capacitor/core: 5.0.3 @capacitor/android: 5.0.3 @capacitor/ios: 5.0.4

Plugin Config

 async initBackgroundFetch() {
    console.log('initBackgroundFetch method Called');
    const status = await BackgroundFetch.configure({
      minimumFetchInterval: 15,
      stopOnTerminate: false,
      enableHeadless: true,
    }, async (taskId) => {
      console.log('[BackgroundFetch] EVENT:', taskId);

      // Add record to list within NgZone
      this.zone.run(() => {
        this.create(taskId, false);
      });

      // Perform your work in an awaited Promise
      const result = await this.performYourWorkHere('BackGroundFetch');

      console.log('[BackgroundFetch] called and response:', result);
      // [REQUIRED] Signal to the OS that your work is complete.
      BackgroundFetch.finish(taskId);
    }, async (taskId) => {
      // The OS has signalled that your remaining background-time has expired.
      // You must immediately complete your work and signal #finish.
      console.log('[BackgroundFetch] TIMEOUT:', taskId);
      // [REQUIRED] Signal to the OS that your work is complete.
      BackgroundFetch.finish(taskId);
    });

    this.state.status = status;
    console.log('Status for BackgroundFetch:-', status)
    // Checking BackgroundFetch status:
    if (status !== BackgroundFetch.STATUS_AVAILABLE) {
      this.state.enabled = false;
      // Uh-oh:  we have a problem:
      if (status === BackgroundFetch.STATUS_DENIED) {
        alert('The user explicitly \n disabled background behavior \n for this app or for \n the whole system.');
      } else if (status === BackgroundFetch.STATUS_RESTRICTED) {
        alert('Background updates \n are unavailable and the user \n cannot enable them again.')
      }
    }
  }

Expected Behavior

It should fetch the location of the device and post it on configure URL if the app is still in the same place and doesn’t change the location within 15 minutes in the three scenarios below listed: . foreground . Background . Killed

Actual Behavior

Unfortunately doesn’t work in any of the above three states of the app in Android, however, iOS only works sometimes in two states if the app is in the foreground or background.

Steps to Reproduce

On an Android device put your device in the same place for more than 15 minutes on any state foreground, background, or kill. On an iOS device put your device in the same place for more than 15 minutes in the kill state

Context

We are a construction company and employees go to their sides with the devices so that employers can track them. Once the employees reach the task destination then they spent a few hours there. During those hours location is not updating. So the employer cannot know if the employee is still on the location.

Debug logs

We have also seen this issue on the Play Store crashes.

com.transistorsoft.tsbackgroundfetch.BGTask$Error

Exception com.transistorsoft.tsbackgroundfetch.BGTask$Error: at com.transistorsoft.tsbackgroundfetch.BGTask.fireHeadlessEvent (BGTask.java:231) at com.transistorsoft.tsbackgroundfetch.BGTask.onTimeout (BGTask.java:196) at com.transistorsoft.tsbackgroundfetch.BGTask$1.run (BGTask.java:80) at android.os.Handler.handleCallback (Handler.java:938) at android.os.Handler.dispatchMessage (Handler.java:99) at android.os.Looper.loopOnce (Looper.java:226) at android.os.Looper.loop (Looper.java:313) at android.app.ActivityThread.main (ActivityThread.java:8751) at java.lang.reflect.Method.invoke at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:571) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1135)

christocracy commented 1 year ago

should fetch the location of the device

No, it should not. BackgroundFetch has nothing to do with “location”.

this plug-in merely executes a periodic callback function, that you provide.

what you do in your callback function is your responsibility (eg using a “geolocation” plug-in to request the current position.)

christocracy commented 1 year ago

com.transistorsoft.tsbackgroundfetch.BGTask.fireHeadlessEvent (BGTask.java:231)

you configured enableHeadless: true: this is a very advanced config option, requiring the creation of a custom Java class. Did you read the api docs about this option?

projul commented 1 year ago

Sure thanks for your quick response!