Last active
September 22, 2020 13:50
-
-
Save mrgulshanyadav/800743c75ce3bf62f22068354272f26e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:workmanager/workmanager.dart'; | |
const myTask = "syncWithTheBackEnd"; | |
void callbackDispatcher() { | |
// this method will be called every hour | |
Workmanager.executeTask((task, inputdata) async { | |
switch (task) { | |
case myTask: | |
print("this method was called from native!"); | |
Fluttertoast.showToast(msg: "this method was called from native!"); | |
break; | |
case Workmanager.iOSBackgroundTask: | |
print("iOS background fetch delegate ran"); | |
break; | |
} | |
//Return true when the task executed successfully or not | |
return Future.value(true); | |
}); | |
} | |
void main() { | |
// needs to be initialized before using workmanager package | |
WidgetsFlutterBinding.ensureInitialized(); | |
// initialize Workmanager with the function which you want to invoke after any periodic time | |
Workmanager.initialize(callbackDispatcher); | |
// Periodic task registration | |
Workmanager.registerPeriodicTask( | |
"2", | |
// use the same task name used in callbackDispatcher function for identifying the task | |
// Each task must have an unique name if you want to add multiple tasks; | |
myTask, | |
// When no frequency is provided the default 15 minutes is set. | |
// Minimum frequency is 15 min. | |
// Android will automatically change your frequency to 15 min if you have configured a lower frequency than 15 minutes. | |
frequency: Duration(hours: 1), // change duration according to your needs | |
); | |
runApp(MyApp()); // at last lines, you can call runApp() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment