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 to schedule a Periodic Task #4

Closed
kterto opened this issue Jul 31, 2019 · 21 comments
Closed

❓How to schedule a Periodic Task #4

kterto opened this issue Jul 31, 2019 · 21 comments
Assignees
Labels
bug Something isn't working question Further information is requested

Comments

@kterto
Copy link

kterto commented Jul 31, 2019

I've been trying to use the package, but I couldn't find out where to put the code to perform the desired Task. As I understood from the working example and from the plugin implementation, I would have to write a custom class that extends the Worker and override the doWork function, but when installing the package by pubspec I wasn't able to find out where to put it.

@timrijckaert
Copy link
Contributor

Hi @kterto, 👋

Thanks for checking out this package.
You are actually the first to open up an issue.

The background job you wish to execute should be written in Dart, you do not need to do any native work to make it work.
Could you describe your use case, so I can understand it better?

@kterto
Copy link
Author

kterto commented Jul 31, 2019

Hi, @timrijckaert, that's my use case:
I would like to perform Firebase requests periodically to check data with the app not executing and trigger an alarm when some condition is met and wake the app.
Thank's for the reply.

@timrijckaert
Copy link
Contributor

timrijckaert commented Jul 31, 2019

Alright, you would want to do something like this:

Define a top level function lets call it callbackDispatcher here, but you could name it whatever you want.
This function will be called in the background.

You need to return either true or false.
Whether your Firebase syncing was successful or not.

void callbackDispatcher() {
  Workmanager.defaultCallbackDispatcher((task) {
    switch (task) {
      case "firebaseSyncing":
        print("sync with Firebase");
        break;
    }

    // Return whether the job ran successful or not.
    return Future.value(true);
  });
}

You then need to initialize the plugin with this function.
In your case you wish to have a task that runs every X time.
Therefore you use the Workmanager#registerPeriodicTask method to schedule a task that will run every X time.
You can configure the time and other constraints (please look in the README).

void main() {
  Workmanager.initialize(callbackDispatcher);
  Workmanager.registerPeriodicTask("1", "firebaseSyncing");
  runApp(MyApp());
}

Let me know if it works.

@kterto
Copy link
Author

kterto commented Jul 31, 2019

Ok, I've been doing something like what you've just said, but where I define the action? Where am I supposed to write the body of the actual function firebaseSyncing that I want to run in the background?

@timrijckaert
Copy link
Contributor

print("sync with Firebase");

@kterto
Copy link
Author

kterto commented Jul 31, 2019

Now the problem is said to be that executeTask is not implemented in the Workmanager.

@timrijckaert
Copy link
Contributor

timrijckaert commented Jul 31, 2019

You are correct, I was referencing the upcoming v0.0.7 API this library.
I corrected the original answer to reflect the required method for v0.0.6 of the library.

@timrijckaert
Copy link
Contributor

Does it work for you now?
Can this be closed?

@timrijckaert timrijckaert added the question Further information is requested label Jul 31, 2019
@timrijckaert timrijckaert self-assigned this Jul 31, 2019
@timrijckaert timrijckaert changed the title How to perform a custom work? ❓How to schedule Periodic Work Jul 31, 2019
@timrijckaert timrijckaert changed the title ❓How to schedule Periodic Work ❓How to schedule a Periodic Task Jul 31, 2019
@kterto
Copy link
Author

kterto commented Jul 31, 2019

I had a problem in the machine and I had to restart... But it's not working yet.
E/MethodChannel#be.tramckrijte.workmanager/foreground_channel_work_manager( 3529): Failed to handle method call

@timrijckaert
Copy link
Contributor

timrijckaert commented Jul 31, 2019

You are most likely running the app on an iOS simulator?
Version v0.0.6 will only work for Android. Therefore you should wrap your calls to check if it runs on Android.

if (Platform.isAndroid) {
  Workmanager.initialize(callbackDispatcher);
  Workmanager.registerPeriodicTask("1", "firebaseSyncing");
}

We are currently working on implementing iOS support in an upcoming version.

Let me know if it works.

@timrijckaert timrijckaert reopened this Jul 31, 2019
@kterto
Copy link
Author

kterto commented Jul 31, 2019

Actually I'm running in my android Mobile, not in a simulator. I've wrapped the code in the if statement and still not working.

@timrijckaert
Copy link
Contributor

Could you send me the code please

@kterto
Copy link
Author

kterto commented Jul 31, 2019

That's what I'm trying to do:

main.txt

@timrijckaert
Copy link
Contributor

Yes I see we have a small bug.
Thanks for reporting this, we will fix it in the upcoming release.
For now you should set the isInDebugMode flag.

void main() async {
  if (Platform.isAndroid) {
    Workmanager.initialize(callbackDispatcher, isInDebugMode: true);
    Workmanager.registerPeriodicTask("1", "firebaseSyncing");
  }
  runApp(App());
}

@timrijckaert timrijckaert added the bug Something isn't working label Jul 31, 2019
@kterto
Copy link
Author

kterto commented Jul 31, 2019

outcome with the isInDebugMode flag:
debugMode.txt

@timrijckaert
Copy link
Contributor

timrijckaert commented Jul 31, 2019

I/flutter ( 6599): syncing with Firebase
I/WM-WorkerWrapper( 6599): Worker result SUCCESS for Work [ id=a54cb75b-569b-45b5-807f-5c4346129bc0, tags={ be.tramckrijte.workmanager.EchoingWorker } ]

It worked!
Congratz 😄

The native crash underneath is not caused by the plugin.

@kterto
Copy link
Author

kterto commented Jul 31, 2019

If I run without the isInDebugMode flag, I still get an error. Is there any way of running in debug mode and not showing the system notification?

@timrijckaert
Copy link
Contributor

timrijckaert commented Jul 31, 2019

You could set the isInDebugMode to false that would work.

I also just updated the plugin to version v0.0.6+1, it fixes the bug.
With v0.0.6+1 you don't need to explicitly set the isDebugMode flag no more.

Workmanager.initialize(callbackDispatcher);

@kterto
Copy link
Author

kterto commented Jul 31, 2019

(sorry to keep reopening this issue, should I use another one?)
Well, when trying to call another plugins, like the firebase_auth, in the callbackDispatcher, I've been facing a problem related to the plugins's own methodChannel... so using functions are not working.
So far I've just succeeded printing to the console some message. I've tried using a local plugin too that I had started just to access native alarm sound and trigger it, it works alone, but in integration with Workmanager it doesn't.
The errors are like this:
[ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: MissingPluginException(No implementation found for method FirebaseApp#appNamed on channel plugins.flutter.io/firebase_core)

@timrijckaert timrijckaert reopened this Aug 1, 2019
@timrijckaert
Copy link
Contributor

I just tested myself with a simple plugin path_provider and I have the same issue.
Seems there is a bug when using background tasks in combination with plugins.

It's better to open up a different issue as this is totally separate bug.
I wonder if it is similar to this issue

@AxesandGrinds
Copy link

Was there a solution to this? Are method calls not working in workmanager?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants