Comments (9)
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.
Can you provide the git source like last time?
from geofence_service.
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.
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.
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.
It would be helpful to solve the problem if you easily explain what function you are trying to implement.
from geofence_service.
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.
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.
Can be closed! Thanks for the help
from geofence_service.
Related Issues (20)
- Updating the data on iOS HOT 1
- Not working when app is removed HOT 4
- Unable to kill app from Paused state in Android HOT 2
- Notification when DWELL HOT 2
- Remove Notification HOT 7
- Multiple _geofenceServices HOT 1
- send text between devices HOT 4
- unable to get any notification HOT 1
- java.lang.IncompatibleClassChangeError: Found interface com.google.android.gms.location.SettingsClient, HOT 3
- Issue with shared_preferences package HOT 2
- fl_location version needs to be updated
- not working in the background
- Geofence is not working on background state HOT 1
- I want to run Geofence_Service in the background HOT 2
- How can I trigger a notfication for the app to open a view
- GeoFence runs in Background
- onGeofenceStatusChanged does not work in release HOT 1
- ForegroundServiceDidNotStartInTimeException - Context.startForegroundService() did not then call Service.startForeground()
- Geofence updates not received when Android app in background - API 33
- Android 14 Foreground service permission
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from geofence_service.