simple_work_manager 0.3.4 simple_work_manager: ^0.3.4 copied to clipboard
Simple Work Manager for Android and iOS.
simple_work_manager #
Simple Work Manager for Android and iOS.
Abstracts #
Simple Work Manager is mapped to Android Work Manager and iOS BGTaskScheduler.
Getting Started #
pubspec.yaml #
Add "simple_work_manager:" into pubspec.yaml.
dependencies:
simple_work_manager:
iOS #
- Permitted background task scheduler identifier in info.plist in Xcode
Set PermittedBackgroundTaskSchedulerIdentifier as unique string like <[com].[domain].simple_work_manager.process>. - Background modes in Xcode
Check Background processing. - Add code for registration into AppDelegate.swift.
GeneratedPluginRegistrant.register(with: self)
Import #
import 'package:simple_work_manager/simple_work_manager.dart';
Preparing callback function #
@pragma("vm:entry-point")
void callbackDispatcher() {
sync_func();
// execute background jobs here.
}
Defines callback function as top level function. The callback function is called in repetition.
Instantiate SimpleWorkManager() #
_simpleWorkManagerPlugin =
SimpleWorkManager(callbackFunction: callbackDispatcher, callbackFunctionIdentifier: "callbackDispatcher");
Specifies callbackFunction defined above. Specifies callbackFunctionIdentifier as arbitrarily non-empty string, typically same as callbackFunction name.
Schedule #
_simpleWorkManagerPlugin.schedule(
AndroidOptions(
requiresNetworkConnectivity: true,
requiresExternalPower: true,
targetPeriodInMinutes: 15),
IOSOptions(
requiresNetworkConnectivity: true,
requiresExternalPower: true,
taskIdentifier: "com.jimdo.uchida001tmhr.simple_work_manager.process"));
requiresNetworkConnectivity and requiresExternalPower must be set as following section. targetPeriodInMinutes specifies minimum interval of background process, minimum value is 15. taskIdentifier must be same as "Permitted background task scheduler identifier" of Info of Xcode. taskIdentifier must be unique, then must contain your domain identifier.
Options(Constraints(Experimental approach)) #
Simple Work Manager can specify the constraints such that requiresNetworkConnectivity and requiresExternalPower. The behaviors according to these constraints are as following tables:
Android #
◯: callback function will be called in background △: callback function will be called but its interval is long ×: callback function wil not be called in background | requiresExternalPower |
|||||||
NULL | FALSE | TRUE |
||||||
Power Plug in | Power Plug off | Power Plug in | Power Plug off | Power Plug in | Power Plug off |
|||
requiresNetworkConnectivity | NULL | WiFi ON | ◯ | △ | ◯ | △ | ◯ | × |
WiFi OFF | ◯ | △ | ◯ | △ | ◯ | × |
||
FALSE | WiFi ON | ◯ | ◯ | ◯ | ◯ | ◯ | × |
|
WiFi OFF | ◯ | △ | ◯ | ◯ | ◯ | × |
||
TRUE | WiFi ON | ◯ | △ | ◯ | △ | ◯ | × |
|
WiFi OFF | × | × | × | × | × | × |
iOS #
◯: callback function will be called in background △: callback function will be called but its interval is long ×: callback function wil not be called in background | requiresExternalPower |
|||||||
NULL | FALSE | TRUE |
||||||
Power Plug in | Power Plug off | Power Plug in | Power Plug off | Power Plug in | Power Plug off |
|||
requiresNetworkConnectivity | NULL | WiFi ON | ◯ | × | ◯ | × | × | × |
WiFi OFF | ◯ | × | ◯ | × | × | × |
||
FALSE | WiFi ON | △ | × | ◯ | × | × | × |
|
WiFi OFF | ◯ | × | △ | × | ◯ | × |
||
TRUE | WiFi ON | ◯ | × | ◯ | × | ◯ | × |
|
WiFi OFF | × | × | × | × | × | × |
SUGGESTION FOR CONSTRAINTS #
Set requiresExternalPower and requiresNetworkConnectivity to TRUE and plug external power IN to execute background process continuously.
Cancel the schedule #
_simpleWorkManagerPlugin.cancel();
CAUTION #
App must not killed to invoke callback function.
In iOS, app must be in background to invoke callback function.