configure static method

Future<int> configure(
  1. BackgroundFetchConfig config,
  2. Function onFetch, [
  3. Function? onTimeout
])

Configures the plugin's BackgroundFetchConfig and callback Function. This callback will fire each time a background-fetch event occurs (typically every 15 min).

BackgroundFetch.configure(BackgroundFetchConfig(
  minimumFetchInterval: 15,
  stopOnTerminate: false,
  startOnBoot: true
), (String taskId) {  // <-- Event callback
  // 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(taskId);
}, (String taskId) async {  // <-- Task timeout
  // This task has exceeded its allowed running-time.  You must stop what you're doing and immediately .finish(taskId)
  BackgroundFetch.finish(taskId);
})

Implementation

static Future<int> configure(BackgroundFetchConfig config, Function onFetch,
    [Function? onTimeout]) {
  if (_eventsFetch == null) {
    _eventsFetch = _eventChannelTask.receiveBroadcastStream();
    if (onTimeout == null) {
      onTimeout = (taskId) {
        print(
            "[BackgroundFetch] task timed-out without onTimeout callback: $taskId.  You should provide an onTimeout callback to BackgroundFetch.configure.");
        finish(taskId);
      };
    }
    _eventsFetch?.listen((dynamic event) {
      String taskId = event['taskId'];
      if (event['timeout']) {
        onTimeout?.call(taskId);
      } else {
        onFetch(taskId);
      }
    });
  }
  Completer completer = Completer<int>();

  _methodChannel
      .invokeMethod('configure', config.toMap())
      .then((dynamic status) {
    completer.complete(status);
  }).catchError((e) {
    print("[BackgroundFetch] ERROR: $e");
    completer.completeError(e);
  });

  return completer.future as Future<int>;
}