Options
All
  • Public
  • Public/Protected
  • All
Menu

Interface TaskConfig

Configuration API for BackgroundFetch.scheduleTask.

BackgroundFetch.scheduleTask({
  taskId: 'my-custom-task',
  delay: 10000,
  periodic: false,
  enableHeadless: true,
  stopOnTerminate: false,
  startOnBoot: true,
  forceAlarmManager: false,
  requiredNetworkType: BackgroundFetch.NETWORK_TYPE_ANY,
  requiresBatteryNotLow: false,
  requiresCharging: false,
  requiresDeviceIdle: false,
  requiresStorageNotLow: false
});

Hierarchy

  • AbstractConfig
    • TaskConfig

Index

Properties

delay

delay: number

The minimum interval in milliseconds to execute this task.

Optional enableHeadless

enableHeadless: boolean

[Android only] Set true to enable Headless mechanism for handling fetch events after app termination.

const status = await BackgroundFetch.configure({
  minimumFetchInterval: 15,
  stopOnTerminate: false,  // <-- required
  enableHeadless: true     // <-- required
}, async (taskId) => {
  console.log('[BackgroundFetch] EVENT', taskId);
  BackgroundFetch.finish(taskId);
}, async (taskId) => {
  console.log('[BackgroundFetch] TIMEOUT', taskId);
  BackgroundFetch.finish(taskId);
})

Android Headless Setup

  • With your app open in Android Studio, browse the app folder to find the MainActivity class. Right-click the containing folder and click New > Java Class.

  • You MUST name the file BackgroundFetchHeadlessTask.

  • Add the following Java code to BackgroundFetchHeadlessTask, taking care to preserve the top line:

package com.your.package.name:

package com.your.package.name  // <-- DO NOT REPLACE THIS LINE!!!!!!!!!!!!

/// ---------------- Paste everything BELOW THIS LINE: -----------------------
import android.content.Context;
import android.util.Log;

import com.transistorsoft.tsbackgroundfetch.BackgroundFetch;
import com.transistorsoft.tsbackgroundfetch.BGTask;

public class BackgroundFetchHeadlessTask{
    public void onFetch(Context context,  BGTask task) {
        // Get a reference to the BackgroundFetch Android API.
        BackgroundFetch backgroundFetch = BackgroundFetch.getInstance(context);
        // Get the taskId.
        String taskId = task.getTaskId();
        // Log a message to adb logcat.
        Log.d("MyHeadlessTask", "BackgroundFetchHeadlessTask onFetch -- CUSTOM IMPLEMENTATION: " + taskId);

        boolean isTimeout = task.getTimedOut();
        // Is this a timeout?
        if (isTimeout) {
          backgroundFetch.finish(taskId);
          return;
        }
        // Do your work here...
        //
        //
        // Signal finish just like the Javascript API.
        backgroundFetch.finish(taskId);
    }
}

Testing Your Headless-task:

After terminating your app with Javascript configuration above, simulate a fetch-event while observing $ adb logcat

In the following command, replace com.your.app.package.name with your app's actual package-name:

adb shell cmd jobscheduler run -f com.your.app.package.name 999

 

$ adb logcat

TSBackgroundFetch: - Background Fetch event received: capacitor-background-fetch
MyHeadlessTask: BackgroundFetchHeadlessTask onFetch -- CUSTOM IMPLEMENTATION: capacitor-background-fetch
TSBackgroundFetch: - finish: capacitor-background-fetch
TSBackgroundFetch: - jobFinished

Optional forceAlarmManager

forceAlarmManager: boolean

[Android only]

By default, the BackgroundFetch Android SDK will use JobScheduler to schedule tasks, which are subject to throttling. For more accurate scheduling of tasks (at the expense of higher battery-usage), you can tell BackgroundFetch to use Android's AlarmManager.

Note: All "criteria" options will no longer apply, since these are available only with JobScheduler.

Optional periodic

periodic: boolean

Whether this task will continue executing or just a "one-shot".

Optional requiredNetworkType

requiredNetworkType: NetworkType

[Android only] Set detailed description of the kind of network your job requires.

If your job doesn't need a network connection, you don't need to use this option, as the default is BackgroundFetch.NETWORK_TYPE_NONE.

Calling this method defines network as a strict requirement for your job. If the network requested is not available your job will never run.

Optional requiresBatteryNotLow

requiresBatteryNotLow: boolean

[Android only] Specify that to run this job, the device's battery level must not be low.

This defaults to false. If true, the job will only run when the battery level is not low, which is generally the point where the user is given a "low battery" warning.

Optional requiresCharging

requiresCharging: boolean

[Android only] Specify that to run this job, the device must be charging (or be a non-battery-powered device connected to permanent power, such as Android TV devices). This defaults to false.

Optional requiresDeviceIdle

requiresDeviceIdle: boolean

[Android only] When set true, ensure that this job will not run if the device is in active use.

The default state is false: that is, the for the job to be runnable even when someone is interacting with the device.

This state is a loose definition provided by the system. In general, it means that the device is not currently being used interactively, and has not been in use for some time. As such, it is a good time to perform resource heavy jobs. Bear in mind that battery usage will still be attributed to your application, and surfaced to the user in battery stats.

Optional requiresStorageNotLow

requiresStorageNotLow: boolean

[Android only] Specify that to run this job, the device's available storage must not be low.

This defaults to false. If true, the job will only run when the device is not in a low storage state, which is generally the point where the user is given a "low storage" warning.

Optional startOnBoot

startOnBoot: boolean

[Android only] Set true to initiate background-fetch events when the device is rebooted. Defaults to false.

Optional stopOnTerminate

stopOnTerminate: boolean

[Android only] Set false to continue background-fetch events after user terminates the app. Default to true.

taskId

taskId: string

The name of the task. This will be used with BackgroundFetch.finish to signal task-completion.

Generated using TypeDoc