androidtypescriptionic-frameworkcapacitorbackground-fetch

ionic backgroundFetch config not updating


I'm building an ionic app with capacitor. I'm hoping that my app can perform background tasks after termination. If i'm reading the documentation correctly this should be possible by setting

stopOnTerminate: false

I've created a blank app that I can test the backgroundFetch plugin on and used the following code.

    const config: BackgroundFetchConfig = {
      stopOnTerminate: false, // Set true to cease background-fetch from operating after user "closes" the app. Defaults to true.
    };

    this.backgroundFetch.configure(config)
        .then(() => {
          console.log('Background Fetch initialized');


          this.backgroundFetch.finish();

        })
        .catch(e => console.log('Error initializing background fetch', e));

    this.backgroundFetch.start();

However when I build the app and open it with capacitor, I can see in the android studio logcat window that it has not updated the stopOnTerminate value

com.example.app D/TSBackgroundFetch: {
      "taskId": "cordova-background-fetch",
      "isFetchTask": true,
      "minimumFetchInterval": 15,
      "stopOnTerminate": true, // This should have changed to false
      "requiredNetworkType": 0,
      "requiresBatteryNotLow": false,
      "requiresCharging": false,
      "requiresDeviceIdle": false,
      "requiresStorageNotLow": false,
      "startOnBoot": false,
      "forceAlarmManager": false,
      "periodic": true,
      "delay": -1
    }

Is there something else I need to do to be able to change the defaults?


Solution

  • Managed to get it working using a different way of importing the BackgroundFetch plugin

    import BackgroundFetch from 'cordova-plugin-background-fetch';
    

    And then in the configure options I needed to add enableHeadless to get it to work

            // Your background-fetch handler.
            const fetchCallback = (taskId) => {
                console.log('[js] BackgroundFetch event received: ', taskId);
                // Required: Signal completion of your task to native code
                // If you fail to do this, the OS can terminate your app
                // or assign battery-blame for consuming too much background-time
                BackgroundFetch.finish(taskId);
            };
    
            const failureCallback = (error) => {
                console.log('- BackgroundFetch failed', error);
            };
    
            BackgroundFetch.configure(fetchCallback, failureCallback, {
                minimumFetchInterval: 15, // <-- default is 15
                stopOnTerminate: false,
                enableHeadless: true // <-- Added this line
            });
    

    Config is now being changed in android studio