안드로이드 및 iOS에서 포그라운드 서비스를 실행하여 지오페싱을 가능하게 하는 플러터 패키지입니다. 앱이 지리적 경계를 모니터링하고 사용자가 정의된 영역에 진입하거나 탈출할 때 작업을 트리거할 수 있으며, 앱이 백그라운드에 있을 때도 작동합니다. 지속적인 알림과 함께 지속적으로 실행되어 플랫폼별 제약 조건에서도 신뢰성을 보장합니다. 실시간 위치 추적을 위해 설계되었으며, 최소한의 설정으로 안드로이드와 iOS 모두를 지원합니다.
사용 사례
실시간 위치 모니터링
소매 또는 물류를 위한 지오페싱 알림
피트니스 앱에서 위치 기반 트리거
배송 서비스를 위한 백그라운드 추적
거리 기반 알림
주요 기능
지속적인 포그라운드 서비스
크로스 플랫폼 Android 및 iOS 지원
필수 구성 최소화
Flutter 위치 서비스와의 통합
진입/출구 지오펜스 이벤트를 지원합니다
적합한 대상
백그라운드 위치 추적을 필요로 하는 앱
위치 인식 애플리케이션
신뢰할 수 있는 지오페싱이 필요한 서비스
물류 또는 유통 솔루션을 개발하는 개발자
엄격한 백그라운드 실행 요구 사항을 가진 프로젝트
고려 사항
위치 액세스를 위해 사용자 권한이 필요합니다
지속적인 알림은 사용자 경험에 영향을 미칠 수 있습니다
지속적인 추적 시 배터리 사용량이 증가합니다
플랫폼별 백그라운드 실행 제한
iOS 백그라운드 모드를 위해 추가 구성이 필요할 수 있습니다
원본 README
연결된 소스 저장소의 README 원문 스냅샷입니다. 최신 내용은 저장소에서 확인하세요.
geofence_foreground_service
A Flutter plugin that enables you to easily handle geofencing events in your Flutter app by utilizing native OS APIs on Android by creating a foreground service while being battery efficient since it uses the Geofence and WorkManager APIs. And on iOS by utilizing the CLLocationManager
It's important to note that the workmanager
and flutter_foreground_task plugins were a
great source of inspiration while creating this plugin.
Android
iOS
Features
Supports geofencing in foreground as well as background 💪
Geofence a circular area 🗺️
Geofence a polygon 🤯 You can add a geofence using a list of coordinates, the system will calculate the center of them and register it, having full polygon support is a WIP 🚧
Notification customization 🔔: ⚠️Android⚠️ Displaying a notification when running a foreground service is mandatory, you can customize what is being displayed on it (title, content or the icon), the plugin displays your app icon by default.
Notification responsiveness ⏱️: ⚠️Android⚠️ You can set the responsiveness of the android notifications as per the docs here
<!--required--><uses-permissionandroid:name="android.permission.FOREGROUND_SERVICE_LOCATION" /><!--at least one of the follwoing--><uses-permissionandroid:name="android.permission.ACCESS_FINE_LOCATION" /><uses-permissionandroid:name="android.permission.ACCESS_COARSE_LOCATION" />
Make sure the minSdkVersion in the app/build.gradle file is 29+
🔧 iOS Setup
Navigate to the Podfile and make sure to set the iOS version to 12+
platform :ios, '12.0'
Make sure to add the following permission to your Info.plist
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>This app need your location to provide best feature based on location</string><key>NSLocationAlwaysUsageDescription</key><string>This app need your location to provide best feature based on location</string><key>NSLocationWhenInUseUsageDescription</key><string>This app need your location to provide best feature based on location</string>
Turn on the Location updates and Background fetch capabilities from XCode
Example
Define the method that will handle the Geofence triggers
Then create an instance of the plugin to initiate it and assign GeoFences to it
finalList<LatLng> timesSquarePolygon = [
constLatLng(40.758078, -73.985640),
constLatLng(40.757983, -73.985417),
constLatLng(40.757881, -73.985493),
constLatLng(40.757956, -73.985688),
];
Future<void> initPlatformState() async {
// Remember to handle permissions before initiating the pluginbool hasServiceStarted = awaitGeofenceForegroundService().startGeofencingService(
contentTitle: 'Test app is running in the background',
contentText: 'Test app will be running to ensure seamless integration with ops team',
notificationChannelId: 'com.app.geofencing_notifications_channel',
serviceId: 525600,
callbackDispatcher: callbackDispatcher,
);
if (hasServiceStarted) {
awaitGeofenceForegroundService().addGeofenceZone(
zone: Zone(
id: 'zone#1_id',
radius: 10000, // measured in meters
coordinates: timesSquarePolygon,
triggers: [
GeofenceEventType.dwell,
GeofenceEventType.enter,
GeofenceEventType.exit
], // Currently, only available on Android
expirationDuration: constDuration(days: 1), // Currently, only available on Android
dwellLoiteringDelay: constDuration(hours: 1), // Currently, only available on Android
initialTrigger: GeofenceEventType.enter, // Currently, only available on Android
),
);
}
}
Something important to point out is the callbackDispatcher method will run in an entirely
different isolate than the actual app, so if you were to handle UI related code inside of it
you'll
need to use Ports, you can find more
information
here
You can pass a custom icon to the foreground service notification if you wish while initializing the
service, this icon will be placed inside the android/app/src/main/res folder, you can check the
example for more information, by default, it will take the app icon
Handling permissions is not a part of the package, so please refer
to permission_handler plugin to grant the required
permissions (it's used in the example too)
location
locationAlways
notification
Contributing
We welcome contributions. Please read CONTRIBUTING.md for pull request expectations (including small PRs and a maximum of 15 files per PR), testing, changelog updates, and review practices.