GithubHelp home page GithubHelp logo

Comments (9)

Dev-hwang avatar Dev-hwang commented on July 1, 2024 1

The WillStartForegroundTask widget serves to help the app enter a background state without exiting when the user tries to close or minimize the app. The background state is when the user navigates from the app to the home screen or turns off the device's screen.

In fact, the WillStartForegroundTask widget has nothing to do with location tracking. It just starts a foreground service, which prevents the app from going into a Doze state and helps it track location in the background. This is a widget you don't need to use unless you're tracking location in the background.

Among the contents of Readme, the code below means the following:

If the user tries to close the app while _geofenceService is running, it starts the foreground service and puts the app in the background state.

@override
Widget build(BuildContext context) {
  return MaterialApp(
    home: WillStartForegroundTask(
      onWillStart: () {
        // You can add a foreground task start condition.
        return _geofenceService.isRunningService;
      },
      androidNotificationOptions: AndroidNotificationOptions(
        channelId: 'geofence_service_notification_channel',
        channelName: 'Geofence Service Notification',
        channelDescription: 'This notification appears when the geofence service is running in the background.',
        channelImportance: NotificationChannelImportance.LOW,
        priority: NotificationPriority.LOW,
        isSticky: false,
      ),
      iosNotificationOptions: IOSNotificationOptions(),
      notificationTitle: 'Geofence Service is running',
      notificationText: 'Tap to return to the app',
      child: Scaffold(
        appBar: AppBar(
          title: const Text('Geofence Service'),
          centerTitle: true,
        ),
        body: _buildContentView(),
      ),
    ),
  );
}

from geofence_service.

Dev-hwang avatar Dev-hwang commented on July 1, 2024

@kamami

Can you provide the git source like last time?

from geofence_service.

kamami avatar kamami commented on July 1, 2024

I tried the whole day to recreate the issue, but I did not manage to do so.
Maybe my undertsanding of this feature is wrong.
Could you explain, what exactly this "WillStartForegroundTask" manages?
Is it repsonsible for my Google Maps to track my location in the background? Or is it only responsible for showing the according notification on my device?

from geofence_service.

kamami avatar kamami commented on July 1, 2024

Thank you, I understand now! Since I want to track the location of the user in the background, this is actually the exact behaviour I need. But I indeed observed, that the _onGeofenceStatusChanged method is called, even if the foreground service was not started, because of my permissions. Do I also need to have some conditions inside of this method? I thought it will automatically not be called, if the foreground service was not started.

from geofence_service.

Dev-hwang avatar Dev-hwang commented on July 1, 2024

The WillStartForegroundTask widget and GeofenceService are separate. Even if the foreground service is not started, GeofenceService will work if it has location permission.

from geofence_service.

Dev-hwang avatar Dev-hwang commented on July 1, 2024

It would be helpful to solve the problem if you easily explain what function you are trying to implement.

from geofence_service.

kamami avatar kamami commented on July 1, 2024

So the general idea is a map with many point of interest with geofences. The user gets a push notification if he enters one of those geofences.

The first screen of the app shows 2 toggles to choose whether or not the app should also track and send out notifications while the app is sleeping. In this scenario the app should stop tracking and sending out notifications while the app is sleeping, because the user only allowed to be tracked while using the app.

Permission handling is already working well! But the logic for the geofence_service when to stop and when to restart the service is giving me headache.
I hope this makes my use case a little more clearer :)

First screen to select the permission (start can only be clicked if at least one is selected)

Second screen: Map with point of interests

from geofence_service.

Dev-hwang avatar Dev-hwang commented on July 1, 2024

How about this way? allowBackground is the value set in the first screen. If the first screen allows background location tracking, it starts the foreground, otherwise it doesn't.

Even if Android has background location permission, location tracking cannot be done in the background unless the foreground service is running. Here's how to take advantage of this limitation.

  @override
  Widget build(BuildContext context) {
    Widget child = Scaffold(
      appBar: AppBar(
        title: const Text('Geofence Service'),
        centerTitle: true,
      ),
      body: Container(),
    );

    if (allowBackground) {
      child = WillStartForegroundTask(
        onWillStart: () async {
          // You can add a foreground task start condition.
          return _geofenceService.isRunningService;
        },
        androidNotificationOptions: const AndroidNotificationOptions(
          channelId: 'geofence_service_notification_channel',
          channelName: 'Geofence Service Notification',
          channelDescription: 'This notification appears when the geofence service is running in the background.',
          channelImportance: NotificationChannelImportance.LOW,
          priority: NotificationPriority.LOW,
          isSticky: false,
        ),
        iosNotificationOptions: const IOSNotificationOptions(),
        notificationTitle: 'Geofence Service is running',
        notificationText: 'Tap to return to the app',
        child: child,
      );
    }

    return MaterialApp(
      home: child,
    );
  }

from geofence_service.

kamami avatar kamami commented on July 1, 2024

Can be closed! Thanks for the help

from geofence_service.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.