SayWut / flutter_foreground_service_plugin

MIT License
12 stars 12 forks source link

bug: startPeriodicTask doesn't always run exactly on the given interval #4

Open toonvanstrijp opened 3 years ago

toonvanstrijp commented 3 years ago

I'm using the startPeriodicTask which works well, but it doesn't always run on the given interval.

I'm setting it up as followed:

FlutterForegroundServicePlugin.startPeriodicTask(
  periodicTaskFun: periodicTaskFun,
  period: const Duration(minutes: 15),
);

Then my periodicTaskFun:

void periodicTaskFun() {
  FlutterForegroundServicePlugin.executeTask(() async {
    // initialize garmin
    print("initializing...");
    await GC.GarminCompanion.init(licenseKey);
    print("initialized");

    if (await GC.GarminCompanion.isBluetoothEnabled()) {
      print("getting devices...");
      final foundDevices = await GC.GarminCompanion.devices();
      // wait for device setup to finish when pairing
      print("waiting for first device...");

      if (foundDevices.length == 0) {
        return await FlutterForegroundServicePlugin
            .refreshForegroundServiceContent(
          notificationContent: NotificationContent(
            iconName: 'ic_launcher',
            titleText: 'Wavy',
            bodyText: 'Connect wearable',
            priority: NotificationPriority.high,
            enableVibration: true,
            enableSound: true,
            color: Colors.blue,
          ),
        );
      }

      final device = foundDevices.first;
      print("device: ${device.address}");
      var events = [];
      StreamSubscription liveDataSubscription = device.liveData([
        GC.RealTimeDataType.HEART_RATE_VARIABILITY,
        GC.RealTimeDataType.HEART_RATE
      ]).listen((event) {
        events.add(event);
        print("data: ${events.length}");
        FlutterForegroundServicePlugin.refreshForegroundServiceContent(
          notificationContent: NotificationContent(
            iconName: 'ic_launcher',
            titleText: 'Wavy',
            bodyText: "data: ${events.length}",
            priority: NotificationPriority.high,
            color: Colors.blue,
          ),
        );
      });
      await Future.delayed(const Duration(minutes: 1), () {});
      await liveDataSubscription.cancel();
    } else {
      await FlutterForegroundServicePlugin.refreshForegroundServiceContent(
        notificationContent: NotificationContent(
          iconName: 'ic_launcher',
          titleText: 'Wavy',
          bodyText: 'Enable bluetooth for wavy to work!',
          priority: NotificationPriority.high,
          color: Colors.blue,
        ),
      );
    }
  });
SayWut commented 3 years ago

Is it not running at all or is it running and get stack after the refreshForegroundServiceContent?

I found a bug that the refreshForegroundServiceContent isn't finishing which makes the thread to wait for the function forever so maybe it because of that but I don't know yet. I will test it and I will let you know

SayWut commented 3 years ago

I think I found the problem, when the OS stops the service the task restart its delay and not continue it from where it stops so if you use a long interval time and the OS kills the service before the task execute it will never run

I will fix this on the next update

SayWut commented 3 years ago

I uploaded a new update (0.1.7) which should fix this problem Let me know if it fixed or not

yakupbaser commented 3 years ago

sorry for not opening new issue but startPeriodikTask is not working when i activate the screen lock. if i close screen lock then startPeriodikTask is going to work well. So is there a way working on screen lock?

toonvanstrijp commented 3 years ago

It's doesn't always work for me, don't really know what the problem could be. But I think it has to do something with the battery optimization done by android. And when the phone is in doze mode it also doesn't run on the given period.

SayWut commented 3 years ago

sorry for not opening new issue but startPeriodikTask is not working when i activate the screen lock. if i close screen lock then startPeriodikTask is going to work well. So is there a way working on screen lock?

It's doesn't always work for me, don't really know what the problem could be. But I think it has to do something with the battery optimization done by android. And when the phone is in doze mode it also doesn't run on the given period.

Which phone do you testing on? Maybe it is because doze mode and standby mode but I don't know yet. It is not easy to test this stuff because every phone works differently. I will do my best

yakupbaser commented 3 years ago

redmi note 4.x.

and one issues too. For example foreground service time duration is 10 seconds. And startPeriodicTask is getting 20 seconds. So foreground service opens a new startPeriodicTask instance. Thts a problem. ForeGround Service must be wait the startPeriodicTask is finished then. Foreground service time duration should be start, i think.

SayWut commented 3 years ago

redmi note 4.x.

and one issues too. For example foreground service time duration is 10 seconds. And startPeriodicTask is getting 20 seconds. So foreground service opens a new startPeriodicTask instance. Thts a problem. ForeGround Service must be wait the startPeriodicTask is finished then. Foreground service time duration should be start, i think.

I fixed this problem in the last update (0.1.7). When the OS stops the service and the timer (task) is running I added a calculation to the new created timer which caclute the delay. For example if the period is 20 min and the service killed after 10 min the delay will be 10 min in the new created timer

SayWut commented 3 years ago

@yakupbaser @ToonvanStrijp I have no idea why your task isn't running I have tested my example app with galaxy 8 and enabled doze mode and standby mode with period of 20 minutes and it executed every time

Can you try to run the example app and see if the task not executing there too? Maybe somthing else in your app is breaking it? try to print the current time as the first line of code in the task method and see if it is printing if it is then the task is running as it should

btw use the latest update