Skip to content

A Flutter plugin that provides general purpose Android services for other plugins to use

License

Notifications You must be signed in to change notification settings

vkammerer/flutter_service

Repository files navigation

Warning: This is an experiment

This project is an attempt at addressing some of the issues discussed in the general thread at flutter/flutter#32164.

I have created it because:

  • it solves a real world issue I have in one of my applications, similar to the one described in this comment.
  • it may help the discussion about what a good long term design could be for background execution in Flutter?

Please open an issue in this repository if you have any comment or suggestion.

What is it?

flutter_service is a Flutter plugin that other plugins can depend on in order to:

  • implement a long running / background execution context for Flutter Dart code on Android.
  • share this execution context with other plugins.

Why?

Flutter exposes low level utilities on Android to execute Dart code, like:

/*
* Simplified example
*/
FlutterEngine flutterEngine = new FlutterEngine(context);
DartExecutor executor = flutterEngine.getDartExecutor();
executor.executeDartCallback(dartCallback);

However, two problems arise when developers implement this pattern themselves:

  • the full implementation is tedious and error prone.
  • it isn't possible for the application plugins to share that execution context.

Current contexts

flutter_services currently exposes two Android execution contexts:

But other contexts could be added in the future, such as

  • WorkManager
  • or other general purpose low level implementations

Usage

Plugin developer

A plugin developer can decide to depend on flutter_service by:

  • adding it as a dependency in pubspec.yaml
  • broadcasting an Intent to
    • com.vincentkammerer.flutter_service.JobIntentServiceBroadcastReceiver
    • or com.vincentkammerer.flutter_service.ForegroundServiceBroadcastReceiver

The signatures of the Intents are currently not documented as they are likely to change but can be seen in the Android implementation code.

User

A user can then implement it in their application by:

  • initializing flutter_service with:
// at least one of the two should be set to true
FlutterService.initialize(
  jobIntentService: true,
  foregroundService: true,
);
  • and then calling the plugins via their own APIs.

Example

The example Flutter project shows how the following plugins could take advantage of flutter_service:

  • android_alarm_manager
  • flutter_local_notifications

In order to make them work with flutter_service, they have been adapted, and the corresponding repositories can be found at:

As the example shows, the following workflow can then occur:

  • flutter_local_notifications_broadcast starts the Foreground Service by broadcasting a notification
  • android_alarm_manager schedules a Dart callback to execute within that Foreground Service at a later time

example diagram for flutter_service

Potential other use cases

While the current example is very basic, it shows how other plugins could benefit from a common background execution context.
For example, firebase_messaging could also execute its dart callbacks within a flutter_service context, and an application could be designed in the following manner:

potential use diagram for flutter_service

Credits

This project is largely an adaptation of the work done in the Flutter Android Alarm Manager plugin, especially the FlutterBackgroundExecutor class.
So most of the original comments have been preserved in the Android code to reflect that legacy.

About

A Flutter plugin that provides general purpose Android services for other plugins to use

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published