This job requires network connectivity.
This job requires network connectivity that is a cellular network.
This job doesn't care about network constraints, either any or none.
This job requires network connectivity that is not roaming.
This job requires network connectivity that is unmetered.
Background fetch is available and enabled.
The user explicitly disabled background behavior for this app or for the whole system.
Background fetch updates are unavailable and the user cannot enable them again. For example, this status can occur when parental controls are in effect for the current user.
Initial configuration of BackgroundFetch, including config-options and Fetch-callback. The start method will automatically be executed.
async initBackgroundFetch() {
const status = await BackgroundFetch.configure({
minimumFetchInterval: 15
}, async (taskId) => { // <---------------- Event handler.
console.log('[BackgroundFetch] EVENT:', taskId);
// Perform your work in an awaited Promise
const result = await this.performYourWorkHere();
console.log('[BackgroundFetch] work complete:', result);
// [REQUIRED] Signal to the OS that your work is complete.
BackgroundFetch.finish(taskId);
}, async (taskId) => { // <---------------- Event timeout handler
// 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);
});
// Checking BackgroundFetch status:
if (status !== BackgroundFetch.STATUS_AVAILABLE) {
// Uh-oh: we have a problem:
if (status === BackgroundFetch.STATUS_DENIED) {
alert('The user explicitly disabled background behavior for this app or for the whole system.');
} else if (status === BackgroundFetch.STATUS_RESTRICTED) {
alert('Background updates are unavailable and the user cannot enable them again.')
}
}
}
// Simulate a long-running task (eg: an HTTP request)
async performYourWorkHere() {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(true);
}, 5000);
});
}
}
You must execute BackgroundFetch.finish(taskId)
within your fetch-callback to signal completion of your task.
If you fail to call .finish()
, the OS will punish your app for poor behaviour and stop firing events.
await BackgroundFetch.configure({
minimumFetchInterval: 15
}, async (taskId) => {
console.log('[BackgroundFetch] EVENT', taskId);
// Always signal completion of your tasks.
BackgroundFetch.finish(taskId);
}, async (taskId) => {
console.log('[BackgroundFetch] TIMEOUT', taskId);
// Always signal completion of your tasks.
BackgroundFetch.finish(taskId);
});
Execute a custom task in addition to the one initially provided to configure. This event can be configured to either a "ONE-SHOT" or "PERIODIC" with TaskConfig.periodic.
// You must ALWAYS first configure BackgroundFetch.
const status = await BackgroundFetch.configure({
minimumFetchInterval: 15
}, async (taskId) => {
console.log('[BackgroundFetch] EVENT', taskId);
if (taskId === 'my-custom-task') {
console.log('Handle your custom-task here');
} else {
console.log('This is the default, periodic fetch task');
}
// Always signal completion of your tasks.
BackgroundFetch.finish(taskId);
}, async (taskId) => {
console.log('[BackgroundFetch] TIMEOUT', taskId);
if (taskId === 'my-custom-task') {
console.log('My custom task timed-out');
} else {
console.log('The default, periodic fetch task timed-out');
}
BackgroundFetch.finish(taskId);
});
// Execute an additional custom-task.
BackgroundFetch.scheduleTask({
taskId: 'my-custom-task', // <-- REQUIRED
delay: 10000, // <-- REQUIRED
periodic: false // <-- ONE-SHOT (default)
})
Start subscribing to fetch events.
Note: The inital call to configure automatically calls BackgroundFetch.start()
async initBackgroundFetch() {
// Calling .configure() automatically starts the plugin.
const status = await BackgroundFetch.configure({
minimumFetchInterval: 15
}, async (taskId) => { // <---------------- Event handler.
console.log('[BackgroundFetch] EVENT:', taskId);
BackgroundFetch.finish(taskId);
}, async (taskId) => { // <---------------- Event timeout handler
console.log('[BackgroundFetch] TIMEOUT:', taskId);
// [REQUIRED] Signal to the OS that your work is complete.
BackgroundFetch.finish(taskId);
});
}
// Stop BackgroundFetch
onClickStop() {
BackgroundFetch.stop();
}
// Re-start BackgroundFetch
onClickStart() {
BackgroundFetch.start();
}
Query the BackgroundFetch API status
// Checking BackgroundFetch status:
const status = await BackgroundFetch.status();
if (status !== BackgroundFetch.STATUS_AVAILABLE) {
// Uh-oh: we have a problem:
if (status === BackgroundFetch.STATUS_DENIED) {
alert('The user explicitly disabled background behavior for this app or for the whole system.');
} else if (status === BackgroundFetch.STATUS_RESTRICTED) {
alert('Background updates are unavailable and the user cannot enable them again.')
}
}
BackgroundFetchStatus | Description |
---|---|
BackgroundFetch.STATUS_RESTRICTED |
Background fetch updates are unavailable and the user cannot enable them again. For example, this status can occur when parental controls are in effect for the current user. |
BackgroundFetch.STATUS_DENIED |
The user explicitly disabled background behavior for this app or for the whole system. |
BackgroundFetch.STATUS_AVAILABLE |
Background fetch is available and enabled. |
Stop subscribing to fetch events.
// Stop everything.
BackgroundFetch.stop();
You may also provide an optional taskId
to stop a scheduleTask:
// Stop a particular task scheduled with BackgroundFetch.scheduleTask
await BackgroundFetch.scheduleTask({
taskId: 'my-custom-task',
delay: 10000,
periodic: false
});
.
.
.
BackgroundFetch.stop('my-custom-task');
Generated using TypeDoc
BackgroundFetch is a module to receive periodic callbacks (min every 15 min) while your app is running in the background or terminated.
iOS
scheduleTask
seems only to fire when the device is plugged into power.stopOnTerminate: false
for iOS.Android