Skip to content

HeadlessEvent

The event object delivered to a BackgroundFetch.registerHeadlessTask handler when the app is running in a terminated (headless) state on Android.

Property Type Description
taskId string Identifier of the task that fired. Pass to BackgroundFetch.finish.
timeout boolean true when the OS signals that background time is nearly exhausted.
Example
// Reflected by TSBackgroundFetch when the app is terminated.
// MUST be named "BackgroundFetchHeadlessTask".
class BackgroundFetchHeadlessTask {
    fun onFetch(context: Context, task: BGTask) {
        val taskId = task.taskId
        if (task.timedOut) {
            BackgroundFetch.getInstance(context).finish(taskId)
            return
        }
        Log.d("MyHeadlessTask", "[BackgroundFetch] headless taskId: $taskId")
        // Perform your work here...
        BackgroundFetch.getInstance(context).finish(taskId)
    }
}

taskId

fun getTaskId(): String

The identifier of the task that fired this headless event.

Pass this value to BackgroundFetch.finish when your work is complete.

timeout

fun getTimedOut(): Boolean

true when the OS signals that background running-time is nearly exhausted.

When a timeout event fires you must immediately call BackgroundFetch.finish and stop any in-progress work. Failure to do so may cause the OS to penalize your app.