Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Long-running workers on Android devices #573

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

wagner1343
Copy link

@wagner1343 wagner1343 commented Oct 21, 2024

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 #236, but goes against the recomendation on #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.

Copy link

docs-page bot commented Oct 21, 2024

To view this pull requests documentation preview, visit the following URL:

docs.page/fluttercommunity/flutter_workmanager~573

Documentation is deployed and generated using docs.page.

@wagner1343 wagner1343 changed the title Long-running workers on Android devices feat: Long-running workers on Android devices Oct 21, 2024
@wagner1343 wagner1343 marked this pull request as ready for review October 21, 2024 14:25
@wagner1343
Copy link
Author

@ened I Kindly ask for your input on this.

Copy link
Contributor

@absar absar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for building these features. I have left some comments

@@ -179,31 +178,32 @@ class Workmanager {
/// The [taskName] is the value that will be returned in the [BackgroundTaskHandler]
/// The [inputData] is the input data for task. Valid value types are: int, bool, double, String and their list
Future<void> registerOneOffTask(
/// Only supported on Android.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably use default dart formatting 2-space indenting, so that it does not change the code which is not modified

required this.notificationChannelDescription,
required this.notificationChannelImportance,
required this.notificationTitle,
required this.notificationDescription});
Copy link
Contributor

@absar absar Nov 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a trailing-comma and reformat, to be consistent

@@ -1,6 +1,7 @@
package dev.fluttercommunity.workmanager

import android.content.Context
import android.content.pm.ServiceInfo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the import used?

@@ -213,13 +255,14 @@ object Extractor {
constraintsConfig = extractConstraintConfigFromCall(call),
outOfQuotaPolicy = extractOutOfQuotaPolicyFromCall(call),
backoffPolicyConfig =
extractBackoffPolicyConfigFromCall(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ened is this formatting ok, or it should be reverted. To not add unncessary changed lines and simplify reviews single formatting should be used

@@ -2,3 +2,6 @@ library workmanager;

export 'src/options.dart';
export 'src/workmanager.dart' show Workmanager;
export 'src/workmanager.dart' show SetForegroundOptions;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead do: export 'src/workmanager.dart' show Workmanager, SetForegroundOptions, etc;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants