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

How can I invoke work manager from native side? #540

Open
RaySuhyunLee opened this issue Mar 2, 2024 · 2 comments
Open

How can I invoke work manager from native side? #540

RaySuhyunLee opened this issue Mar 2, 2024 · 2 comments

Comments

@RaySuhyunLee
Copy link

RaySuhyunLee commented Mar 2, 2024

First of all, thank you for such a great plugin :)

I'm wondering if it's possible to invoke the work manager from native side, just like in flutter.
For instance, I want to do something like this (in android):

class MainActivity : FlutterActivity() {
    ...
    // triggered by some other native code
    fun triggerBackgroundWork() {
        final inputData = [workDataOf("param" to "foo")]
        FlutterWorkManager().registerOneOffTask("unique-task-id", "myTask", inputData: inputData)
    }

which triggers callback dispatcher defined in flutter side.

I first considered to manually instantiate a BackgroundWorker, but I'm not sure how I can import this class from my native code (or if it's even possible)

Is there any way to do this, or do you have any plans to support it in the future?
Thanks in advance.

@ened
Copy link
Collaborator

ened commented Mar 2, 2024

What is your usecase for this?

@RaySuhyunLee
Copy link
Author

RaySuhyunLee commented Mar 3, 2024

Summary: I have to trigger background worker from an Invisible Activity (without UI).

This is my use case:

  • Suppose user is using another app, and wants to share something (e.g., text) to my app.
  • Normally, when something is shared via intent, my app will launch and the screen would switch to my app.
  • But since I don't want to distract the user, I want to just run a background process without making any UI change.

To do this, I made an invisible activity by using @android:style/Theme.NoDisplay, as invisible activity doesn't trigger app switching animation. This makes things a little bit complicated. Unlike normal activities, invisible activities should finish themselves right after being created (or they will crash). So to summarize, ideal workflow is like this:

  1. Invisible activity launches
  2. The activity triggers the flutter-side background worker
  3. The activity forgets about background worker and finish in advance.
  4. The flutter background worker starts, do some stuff and quit silently.

But since this doesn't seem possible for now, I'm using the following workaround, which is very hacky 😂

/* Android-side */
class TransparentActivity : FlutterActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        final methodChannel = ...
        final arguments = ...
        methodChannel.invokeMethod("triggerWorker", arguments) // ask for flutter worker to run with given arguments
        sleep(5000) // Wait until flutter engine to attach (so that it can receive method call)
        finish()
    }
}
/* Flutter-side */
void main() async {
  Workmanager().initialize(callbackDispatcher);
  final methodChannel = ...
  methodChannel.setMethodCallHandler((call) async {
    final inputData = {'arguments': call.arguments};
    Workmanager().registerOneOffTask(Uuid().v4(), "backgroundWork", inputData: inputData);
  });
  runApp(MyApp());
}

@pragma('vm:entry-point')
void callbackDispatcher() {
  ...
}

I know that my use case is very specific and complicated, but it will be really handy if there is a better approach.

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

No branches or pull requests

2 participants