flutter_workmanager
flutter_workmanager copied to clipboard
WorkManger stops executing task after app is closed
The background task is executing as expected as long as the app is open, however, when I close the app (not minimize). The background task finishes too.
Sample code:
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:workmanager/workmanager.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
Workmanager().initialize(
callbackDispatcher,
isInDebugMode: true,
);
runApp(MyApp());
}
void callbackDispatcher() {
Workmanager().executeTask((task, inputData) async {
await _simpleTask();
return Future.value(true);
});
}
Future _simpleTask() async {
await Future.forEach([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], (e) async {
final message = 'Message $e';
print(message);
Fluttertoast.showToast(
msg: message,
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.CENTER,
timeInSecForIosWeb: 1,
backgroundColor: Colors.red,
textColor: Colors.white,
fontSize: 16.0);
await Future.delayed(Duration(seconds: 5));
});
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("Flutter WorkManager Example"),
),
body: ElevatedButton(
child: Text("Register OneOff Task"),
onPressed: () {
Workmanager().registerOneOffTask(
'1',
'task',
);
},
),
),
);
}
}
Tested on the physical device. Pixel 2, Android P
@qbait can you please take logs using pidcat or adb logcat and post them here? Also, a sample project would be super useful. Finally, please send details on your phone, OS etc.
Facing same issue when app is closed, callbackDispatcher is not called when app is closed. when we reopen the app than callbackDispatcher is triggered.
Same issue
Same issue.
Same issue.
Any updates?
The same problem, if the task is not executed for a long time, the task will be executed immediately after starting the APP.
use corn package
same issue for me. after i put the device to idle
Same problem. @ened Will any work be put towards this issue in the near future ?
Same issue
I mean, this completely breaks the functionality of the plugin. If it's not triggering when I have the app closed, what is this whole thing good for? Why isn't any of this being worked on, is the whole project abandoned?
Is no work going to be done on this? currently this package provides 0 advantage as we can simply run cron if we wanted to schedule stuff. I thought this would actually work without relying on whether is app is open or not? Atleast the oneOffTask should have consistent behavior.
As I found out, at least on Android this is the expected behavior.
I switched to background_fetch, which works pretty well. That being said I can however confirm the previous statement from TheCarpetMerchant. The user closing the app ultimately means "do not ever run this application in the background". Plus every OEM producing Android adds some extra energy saving algorithm that reduces/prevents background execution. That's a limitation none of the plugins can avoid.
Are there any updates about this issue?
I did some tests for my app, on 7 Android devices, from Android 5 to Android 14. From my obervations, the behaviour depends only on the manufacturer, not on code or Android version. (It even looks like Android < 7 has a periodicity very accurate, while after, the OS becomes more smart to fluctuate +/- minutes compared to the theoric duration).
- For most of them, when you put the app background OR when you kill the app, the periodic tasks still process in background (tested mainly on Sony phones, Google Pixel).
- But very few (in my case, a Blackview tablet, with not stock Android), the periodic tasks stop working when the app is killed or even in background. For this case, i tried removing battery optimization, data optimization, nothing worked : when the app is not running, the OS stops processing periodic tasks until next launch. The only solution I found to keep the periodic tasks on this device, is a "lock app" feature (), that forbid to kill the app, and the backgroud periodic tasks always occur.
Same conclusions by other people here : https://stackoverflow.com/questions/55349488/work-manager-not-working-when-app-is-killed-by-the-user-why