BackgroundFetch class

BackgroundFetch API

Background Fetch is a very simple plugin which will awaken an app in the background about every 15 minutes, providing a short period of background running-time. This plugin will execute your provided callbackFn whenever a background-fetch event occurs.

There is no way to increase the rate which a fetch-event occurs and this plugin sets the rate to the most frequent possible — you will never receive an event faster than 15 minutes. The operating-system will automatically throttle the rate the background-fetch events occur based upon usage patterns. Eg: if user hasn't turned on their phone for a long period of time, fetch events will occur less frequently.

The Android plugin provides an BackgroundFetchConfig.enableHeadless mechanism allowing you to continue handling events even after app-termination (see BackgroundFetchConfig.enableHeadless).

BackgroundFetch.configure(BackgroundFetchConfig(
  minimumFetchInterval: 15,
  stopOnTerminate: false,
  startOnBoot: true
), () {
  // This callback is typically fired every 15 minutes while in the background.
  print('[BackgroundFetch] Event received.');
  // IMPORTANT:  You must signal completion of your fetch task or the OS could
  // punish your app for spending much time in the background.
  BackgroundFetch.finish();
})

Constructors

BackgroundFetch()

Properties

hashCode → int
The hash code for this object.
read-only, inherited
runtimeType → Type
A representation of the runtime type of the object.
read-only, inherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed.
inherited
toString() → String
Returns a string representation of this object.
inherited

Operators

operator ==(dynamic other) → bool
The equality operator.
inherited

Static Properties

status → Future<int>
read-only

Static Methods

configure(BackgroundFetchConfig config, Function callback) → Future<int>
Configures the plugin's BackgroundFetchConfig and callback Function. This callback will fire each time a background-fetch event occurs (typically every 15 min). [...]
finish([int fetchResult ]) → void
Signal to the OS that your fetch-event is complete. [...]
registerHeadlessTask(Function callback) → Future<bool>
Android-only: Registers a global function to execute when your app has been terminated. [...]
start() → Future<int>
Start the background-fetch API. [...]
stop() → Future<int>
Stop the background-fetch API from firing fetch events. [...]

Constants

FETCH_RESULT_FAILED → const int
See finish. An attempt to download data was made but that attempt failed.
2
FETCH_RESULT_NEW_DATA → const int
See finish. New data was successfully downloaded.
0
FETCH_RESULT_NO_DATA → const int
See finish. There was no new data to download.
1
STATUS_AVAILABLE → const int
See status. Background updates are available for the app.
2
STATUS_DENIED → const int
See status. The user explicitly disabled background behavior for this app or for the whole system.
1
STATUS_RESTRICTED → const int
See status. Background 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.
0