-
Notifications
You must be signed in to change notification settings - Fork 276
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
Comments
What is your usecase for this? |
Summary: I have to trigger background worker from an Invisible Activity (without UI). This is my use case:
To do this, I made an invisible activity by using
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. |
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):
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.
The text was updated successfully, but these errors were encountered: