workmanager
The Aurora implementation of the workmanager.
Features
- registerOneOffTask
- registerPeriodicTask
- cancelByUniqueName
- cancelByTag
- cancelAll
Limitations
- Dart entry point for handling tasks should be called
callbackDispatcher. - One-off tasks do not try to restart on error (no backoff support).
- The constraints are set in the application's desktop file, not through the dart code.
- Initial delay in periodic tasks is not supported.
- Periodic tasks are started immediately upon registration.
- Minimum frequency of periodic tasks is 15 minutes.
- In order to test background tasks for Aurora please run your application from app icon.
flutter runwon't work due to it doesn't use Runtime manager. - Tasks are launched as a separate process with its own Dart VM and does not have access to the application process memory.
Usage
pubspec.yaml
dependencies:
workmanager: ^0.5.2
CMakeLists.txt
find_package(Qt5 COMPONENTS Core REQUIRED)
target_link_libraries(${BINARY_NAME} PRIVATE Qt5::Core)
*main.cpp
#include <flutter/application.h>
#include <flutter/flutter_compatibility_qt.h> // <- Enable Qt
#include "generated_plugin_registrant.h"
int main(int argc, char *argv[]) {
aurora::Initialize(argc, argv);
aurora::EnableQtCompatibility(); // <- Enable Qt
aurora::RegisterPlugins();
aurora::Launch();
return 0;
}
*.desktop
In Aurora OS, background tasks need to be declared in a desktop file.
The mandatory field is Type, where you should specify worker for one-of tasks and periodic for periodic tasks.
You can also optionally specify the permissions that your background task needs through an enumeration in the Permissions field.
[X-Task <ONE_OF_TASK_NAME>]
Type=worker
Permissions=
[X-Task <PERIODIC_TASK_NAME>]
Type=periodic
Permissions=Location
You can also optionally specify the conditions of the background task. For example, if your background task requires the Internet, you can specify the following in the Conditions field:
[X-Task <PERIODIC_TASK_WITH_INTERNET_NAME>]
Type=periodic
Permissions=Internet
Conditions=internet
*.dart
import 'package:workmanager/workmanager.dart';
@pragma('vm:entry-point')
void callbackDispatcher() {
Workmanager().executeTask((task, inputData) {
switch (task) {
case "SimpleTask":
print("Background task started: $task");
break;
default:
return Future.value(false);
}
return Future.value(true);
});
}
void main() {
WidgetsFlutterBinding.ensureInitialized();
Workmanager().initialize(callbackDispatcher, isInDebugMode: true);
Workmanager().registerOneOffTask("unique-task-id", "SimpleTask");
runApp(MyApp());
}