W

workmanager_aurora

workmanager_aurora

The Aurora implementation of the workmanager.

Features

  • registerOneOffTask
  • registerPeriodicTask
  • cancelByUniqueName
  • cancelByTag
  • cancelAll

Limitations

  1. Dart entry point for handling tasks should be called callbackDispatcher.
  2. One-off tasks do not try to restart on error (no backoff support).
  3. The constraints are set in the application's desktop file, not through the dart code.
  4. Initial delay in periodic tasks is not supported.
  5. Periodic tasks are started immediately upon registration.
  6. Minimum frequency of periodic tasks is 15 minutes.
  7. In order to test background tasks for Aurora please run your application from app icon. flutter run won't work due to it doesn't use Runtime manager.

Usage

This package is not an endorsed implementation of workmanager. Therefore, you have to include workmanager_aurora alongside workmanager as dependencies in your pubspec.yaml file.

pubspec.yaml

dependencies:
  workmanager: ^0.5.2
  workmanager_aurora:
    git:
      url: https://gitlab.com/omprussia/flutter/flutter-community-plugins/workmanager_aurora.git
      ref: aurora-0.0.1

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

*.spec

BuildRequires: pkgconfig(appdir-cpp)
BuildRequires: pkgconfig(runtime-manager-qt5)
BuildRequires: pkgconfig(nemonotifications-qt5)

*.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());
}