flutter_workmanager
flutter_workmanager copied to clipboard
feat: Long-running workers on Android devices
This pull request introduces foreground service support on Android devices as per Android workmanager docs Support for long-running workers.
Foreground service support:
- Added new methods
createForegroundInfoandcreateNotificationChannelinBackgroundWorker.ktto handle the creation of foreground notifications and channels. - Introduced
SetForegrounddata class andparseSetForegroundCallmethod inExtractor.ktto parse method calls related to setting foreground options. [1] [2] - Added
setForegroundmethod andSetForegroundOptionsclass inworkmanager.dartto set foreground options from Dart code. [1] [2]
Code refactoring and improvements:
- Refactored
onMethodCallmethod inBackgroundWorker.ktto handle the newSET_FOREGROUNDmethod call. - Updated imports and removed redundant imports in
BackgroundWorker.ktandExtractor.kt. [1] [2]
These changes allows tasks to run as foreground services.
This PR addresses issue https://github.com/fluttercommunity/flutter_workmanager/issues/236, but goes against the recomendation on https://github.com/fluttercommunity/flutter_workmanager/pull/511 of using Workmanager.registerProcessingTask to implement long-running wokers on android.
Since the implementation of long-running workers on Android tightly depends on specific requirements such as handling notifications and activating a foreground service on the run, a decision was made to create a dedicated API which laverages ListenableWorker.serForegroundAsync and minimally handles notification management in order to implement long-running workers while following android guidelines.