flutter_workmanager icon indicating copy to clipboard operation
flutter_workmanager copied to clipboard

feat: Long-running workers on Android devices

Open wagner1343 opened this issue 1 year ago • 2 comments

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 createForegroundInfo and createNotificationChannel in BackgroundWorker.kt to handle the creation of foreground notifications and channels.
  • Introduced SetForeground data class and parseSetForegroundCall method in Extractor.kt to parse method calls related to setting foreground options. [1] [2]
  • Added setForeground method and SetForegroundOptions class in workmanager.dart to set foreground options from Dart code. [1] [2]

Code refactoring and improvements:

  • Refactored onMethodCall method in BackgroundWorker.kt to handle the new SET_FOREGROUND method call.
  • Updated imports and removed redundant imports in BackgroundWorker.kt and Extractor.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.

wagner1343 avatar Oct 21 '24 13:10 wagner1343