Flutter_background_fetch

flutter_background_fetch

The flutter_background_fetch package is a Flutter plugin that allows you to run code in the background of your app. With this package, you can schedule tasks and execute them even when your app is not actively in use or if the device is locked.

Features

  • Background execution of tasks
  • Customizable task schedules
  • Foreground and background notifications
  • Support for both Android and iOS platforms

Getting Started

To use the flutter_background_fetch package, follow these steps:

  1. Import the package:
<!-- In your pubspec.yaml -->
dependencies:
  flutter_background_fetch: ^0.7.0

Make sure to check the latest version on the package’s pub.dev page.

  1. Initialize the package:
// Import the package
import 'package:flutter_background_fetch/flutter_background_fetch.dart';

void main() {
  // Register the background fetch callback
  BackgroundFetch.registerHeadlessTask(backgroundFetchHeadlessTask);

  runApp(MyApp());
}

// Define the background fetch task
void backgroundFetchHeadlessTask(String taskId) async {
  // Perform your background tasks here
  print("Background fetch task executed with ID: \$taskId");

  // You can also perform network requests or any other logic
  // Remember to keep your tasks small and efficient
}

This code registers your background fetch callback and defines the task that will be executed when the background fetch is triggered.

Note that the background fetch task should be as quick and efficient as possible, as there are limitations on how much time can be spent in the background.

  1. Start the background fetch:
void startBackgroundFetch() {
  BackgroundFetch.start().then((int status) {
    print("Background fetch started with status: \$status");
  });
}

You can call the start() method to initiate the background fetch. The returned status value indicates if the background fetch was successfully started or not.

  1. Configure the background fetch:
void configureBackgroundFetch() {
  BackgroundFetch.configure(
    BackgroundFetchConfig(
      minimumFetchInterval: 15, // Minimum fetch interval in minutes (Android only)
      stopOnTerminate: false, // Continue background fetch even when the app is terminated (Android only)
      enableHeadless: true, // Enable headless background fetch tasks (Android only)
    ),
    (String taskId) async {
      // Perform your background tasks here
      print("Background fetch task executed with ID: \$taskId");

      BackgroundFetch.finish(taskId);
    },
  );
}

This code shows how to configure the background fetch, including setting the minimum fetch interval, enabling headless tasks, and handling the execution of the background fetch task. The finish() method is called to mark the task as completed.

Remember to call the startBackgroundFetch() and configureBackgroundFetch() methods from appropriate places in your app’s code, such as the initState() method of your main widget.

Usage with Foreground Notifications

The flutter_background_fetch package also provides support for displaying foreground notifications during the background fetch. This can be useful to inform the user about the progress or results of the background task.

void configureBackgroundFetchWithNotifications() {
  BackgroundFetch.configure(
    BackgroundFetchConfig(
      minimumFetchInterval: 15,
      stopOnTerminate: false,
      enableHeadless: true,
      notifications: BackgroundFetchNotifications(
        title: 'Background Fetch',
        text: 'Performing background task...',
        subText: 'Please wait...',
      ),
    ),
    (String taskId) async {
      // Perform your background tasks here
      print("Background fetch task executed with ID: \$taskId");

      BackgroundFetch.finish(taskId);
    },
  );
}

This code demonstrates how to configure the background fetch with foreground notifications. You can customize the title, text, and subText of the notification to fit your app’s needs.

Conclusion

The flutter_background_fetch package allows you to easily run code in the background of your Flutter app, even when it’s not active or when the device is locked. By scheduling tasks and configuring background fetch, you can perform background operations efficiently and improve the user experience of your app.

Leave a comment