fluttercommunity / flutter_workmanager

A Flutter plugin which allows you to execute code in the background on Android and iOS.
842 stars 255 forks source link

WorkManger stops executing task after app is closed #363

Open qbait opened 2 years ago

qbait commented 2 years ago

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

ened commented 2 years ago

@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.

AnilNeodove commented 2 years ago

Facing same issue when app is closed, callbackDispatcher is not called when app is closed. when we reopen the app than callbackDispatcher is triggered.

yusriltakeuchi commented 2 years ago

Same issue

crcisnabi commented 2 years ago

Same issue.

AnasSafi commented 2 years ago

Same issue.

AErmek commented 2 years ago

Any updates?

xiaoyaoking commented 2 years ago

The same problem, if the task is not executed for a long time, the task will be executed immediately after starting the APP.

msnmuhsinpt commented 2 years ago

use corn package

razfazz commented 2 years ago

same issue for me. after i put the device to idle

TheCarpetMerchant commented 2 years ago

Same problem. @ened Will any work be put towards this issue in the near future ?

gzsendiEhc commented 2 years ago

Same issue

jaded0 commented 2 years ago

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?

SupremeDeity commented 1 year ago

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.

TheCarpetMerchant commented 1 year ago

As I found out, at least on Android this is the expected behavior.

mrclauss commented 1 year ago

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.

eakarpov commented 10 months ago

Are there any updates about this issue?

tototo23 commented 7 months ago

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).

Same conclusions by other people here : https://stackoverflow.com/questions/55349488/work-manager-not-working-when-app-is-killed-by-the-user-why