flutter_background 1.0.0 copy "flutter_background: ^1.0.0" to clipboard
flutter_background: ^1.0.0 copied to clipboard

outdated

A plugin to keep flutter apps running in the background by using foreground service, wake lock and disabling battery optimizations

flutter_background #

A plugin to keep flutter apps running in the background. Currently only works with Android.

It achieves this functionality by running an Android foreground service in combination with a partial wake lock and disabling battery optimizations in order to keep the flutter isolate running.

Note: This plugin currently only works with Android. PRs for iOS are very welcome, although I am not sure if a similiar effect can be achieved with iOS at all.

Getting started #

To use this plugin, add flutter_background as a dependency in your pubspec.yaml file.

Android #

Add the following permissions to the AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="de.julianassmann.flutter_background_example">

    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
    <uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />

    <application>
    ...
    </application>
</manifest>

iOS #

iOS is currently not supported.

Usage #

Import flutter_background.dart:

import 'package:flutter_background/flutter_background.dart';

Initializing plugin and handling permissions #

Before you can use this plugin, you need to initialize it by calling FlutterBackground.initialize(...):

final androidConfig = FlutterBackgroundAndroidConfig(
    notificationTitle: "Title of the notification",
    notificationText: "Text of the notification",
    notificationImportance: AndroidNotificationImportance.Default,
    notificationIcon: AndroidResource(name: 'background_icon', defType: 'drawable'), // Default is ic_launcher from folder mipmap
);
bool success = await FlutterBackground.initialize(androidConfig: androidconfig);

This ensures all permissions are granted and requests them if necessary. It also configures the foreground notification. The configuration above results in the foreground notification shown below when running FlutterBackground.enableBackgroundExecution().

The foreground notification created by the code above.

The notification icon is for the small icon displayed in the top left of a notification and must be a drawable Android Resource (see here for more). In this example, background_icon is a drawable resource in the drawable folders (see the example app). For more information check out the Android documentation for creating notification icons for more information how to create and store an icon.

In order to function correctly, this plugin needs a few permissions. FlutterBackground.initialize(...) will request permissions from the user if necessary. You can call initialize more than one time, so you can call initalize() every time before you call enableBackgroundExecution() (see below).

In order to notify the user about upcoming permission requests by the system, you need to know, whether or not the app already has these permissions. You can find out by calling

bool hasPermissions = await FlutterBackground.hasPermissions;

before calling FlutterBackground.initialize(...). If the app already has all necessary permissions, no permission requests will be displayed to the user.

Run app in background #

With

bool success = await FlutterBackground.enableBackgroundExecution();

you can try to get the app running in the background. You must call FlutterBackground.initialize() before calling FlutterBackground.enableBackgroundExecution().

With

await FlutterBackground.disableBackgroundExecution();

you can stop the background execution of the app. You must call FlutterBackground.initialize() before calling FlutterBackground.disableBackgroundExecution().

To check whether background execution is currently enabled, use

bool enabled = FlutterBackground.isBackgroundExecutionEnabled

Example #

The example is a TCP chat app: It can connect to a TCP server and send and receive messages. The user is notified about incoming messages by notifications created with the plugin flutter_local_notifications.

Using this plugin, the example app can maintain the TCP connection with the server, receiving messages and creating notifications for the user even when in the background.

Maintainer #

Julian Aßmann

If you experience any problems with this package, please create an issue on Github. Pull requests are also very welcome.

313
likes
0
pub points
97%
popularity

Publisher

verified publisherjulianassmann.de

A plugin to keep flutter apps running in the background by using foreground service, wake lock and disabling battery optimizations

Homepage
Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter

More

Packages that depend on flutter_background