Skip to content

Foreground service stops on killing the application #3

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

Closed
darshansatra1 opened this issue Jun 18, 2021 · 10 comments
Closed

Foreground service stops on killing the application #3

darshansatra1 opened this issue Jun 18, 2021 · 10 comments

Comments

@darshansatra1
Copy link

No description provided.

@darshansatra1
Copy link
Author

I tried this flutter_foreground_service_plugin plugin and it luckily worked. It would be great if you can implement this behaviour in your plugin. Your plugin is built very nicely and thanks for building it, although this is the limitation for me that I can not ignore.

@Dev-hwang
Copy link
Owner

Thank you. I checked the plugin. It seems to be possible to implement using PluginUtilities. Let's test and implement PluginUtilities.

@darshansatra1
Copy link
Author

Any updates? @Dev-hwang

@Dev-hwang
Copy link
Owner

Any updates? @Dev-hwang

I'm busy working on other projects. Will update in the near future.

@Dev-hwang
Copy link
Owner

@darshansatra1 The plugin has been updated. There are many changes, so please refer to the readme.

@TheLastGimbus
Copy link

Hi there! How did you fix this? For me, service still closes when I swipe the app out from "recents" 😕 I have pure stock android... I tried adding WithForegroundTask (no idea how it works but okay), didn't help :(

@Dev-hwang
Copy link
Owner

@TheLastGimbus
Try removing the stopWithTask setting in the service tag.

@TheLastGimbus
Copy link

TheLastGimbus commented Jul 16, 2021

Jeez this was so obvious, thanks!!

Um by the way, it looks like hot-reload/even restart doesn't work for it at all D: I need to re-install the whole app for it to work :( Is this normal? Honestly it's okay if it's required - in exchange for working plugin 💖 there are so many of them and none work 😖

Edit: Okay, it's because the code is running in different Isolate, and this seems to be long-known flutter issue - can I somehow kill that Isolate to create a new one every time i stop and start the service?

Oh and if may I ask one more question - I wanted to have my code work in more like "lifecycle" way - set things up before FlutterForegroundTask.initDispatcher, and then close them in onDestroy, like this:

void globalMethod() async {
  StreamSubscription stream =
      Stream.periodic((const Duration(seconds: 1))).listen((event) async {
    print("Stream :DD");
    await FlutterForegroundTask.update(
      notificationTitle: "Doing something...",
      notificationText: event.toString(),
    );
  });
  FlutterForegroundTask.initDispatcher(
    (timestamp) async {
      // Nothing in here
    },
    onDestroy: (timestamp) async {
      await stream.cancel();
      print('onDestroy D:');
    },
  );
}

Everything should work, right? It does now when I try it, tho I wanted to ask because maybe you have some bad experiences or smth :P

@Dev-hwang
Copy link
Owner

@TheLastGimbus
Data and event handling must only be done inside the FlutterForegroundTask.initDispatcher function.
If you don't know exactly what you want, but want to subscribe to the stream, try this method.

void callback() {
  final positionStream = Geolocator.getPositionStream();
  StreamSubscription<Position>? streamSubscription;

  FlutterForegroundTask.initDispatcher((timestamp) async {
    if (streamSubscription != null) return;

    streamSubscription = positionStream.listen((event) {
      print('timestamp: ${timestamp.toString()}');

      FlutterForegroundTask.update(
          notificationTitle: 'Current GPS localization',
          notificationText: '${event.latitude}, ${event.longitude}');
    });
  }, onDestroy: (timestamp) async {
    await streamSubscription?.cancel();
    print('callback() is dead.. x_x');
  });
}

@TheLastGimbus
Copy link

Thanks! I'll do it like this

About hot-reload: I tried Isolate.current.kill() at the and of onDestroy - didn't work 😞 if you had any idea, I would be very grateful (and had nice couple of hours wasted on re-installing it less :P )

Nevertheless - thanks very much for this plugin! Something finally works!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants