This is an archived project and is no longer supported or updated. Please do not file issues or pull-requests against this repo. If you wish to continue to develop this code yourself, we recommend you fork it.
A Plugin for all your geofence interactions.
We have a minimum requirement of iOS 9.0.
In your Info.plist
you'll need to add following keys:
- NSLocationWhenInUseUsageDescription
- NSLocationAlwaysAndWhenInUseUsageDescription
If you using iOS 14 and later you need to add to following to your Info.plist
to register observatory port with mDNS.
<key>NSBonjourServices</key>
<array>
<string>_dartobservatory._tcp</string>
</array>
To avoid your app from craching when you hot restart your app you need to enable background capabilities. If you need this functionalities in production, you will need to explain to Apple why you need background capabilities otherwise, you app will be rejected from the appStore. The following will add background capabilities to your app.
<key>NSLocationAlwaysUsageDescription</key>
<string>I want to get your location Information in background</string>
<key>UIBackgroundModes</key>
<array>
<string>location</string>
</array>
However, remember to remove the above two keys if you don't need background capabilities in production.
In your AndroidManifest.xml
you should add the following lines:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
If you want to build for android 10+ you need to add ACCESS_BACKGROUND_LOCATION too.
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
You'll need to register for a background service as well.
<receiver android:name="com.intivoto.flutter_geofence.GeofenceBroadcastReceiver"
android:enabled="true" android:exported="true"/>
<service android:name="com.intivoto.flutter_geofence.FlutterGeofencePlugin"
android:permission="android.permission.BIND_JOB_SERVICE" android:exported="true"/>
This should be sufficient if your app runs with the new Android embedding (v2, Flutter 1.12), also see: https://flutter.dev/docs/development/packages-and-plugins/plugin-api-migration.
There is a small example app that prints to console to give an example of functionality of this plugin. At the current time following commands are supported.
Currently you need to initialize the plugin to let it setup it's system. This might be altered in later versions.
Geofence.initialize();
In first versions of the plugin permissions were automatically handled, but as the user of the plugin might want more control over this (or implement it theirselves), this was moved to a separate call.
Geofence.requestPermissions();
This will give you a Future
which will resolve with the current Coordinate
of the user. If there is one that's recent enough (< 60 seconds old), it'll return this location.
Example usage:
Geofence.getCurrentLocation().then((coordinate) {
print("Your latitude is ${coordinate.latitude} and longitude ${coordinate.longitude}");
});
This gives you the possibility to add regions to the native geofence manager. Note that even though by adding them they will get triggered, nothing will happen in your app just yet. You also need to start listening to them. You'll get a callback to tell you that your region has been scheduled though.
Geofence.addGeolocation(location, GeolocationEvent.entry).then((onValue) {
scheduleNotification("Georegion added", "Your geofence has been added!");
}).catchError((error) {
print("failed with $error");
});
This gives you the possibility to remove regions you earlier added. Use it like:
Geofence.removeGeolocation(location, GeolocationEvent.entry).then((onValue) {
scheduleNotification("Georegion removed", "Your geofence has been removed!");
}).catchError((error) {
print("failed with $error");
});
Start listening to geoevents as they happen.
NOTE: Region events may not happen immediately after a region boundary is crossed. To prevent spurious notifications, iOS doesn’t deliver region notifications until certain threshold conditions are met. Specifically, the user’s location must cross the region boundary, move away from the boundary by a minimum distance, and remain at that minimum distance for at least 20 seconds before the notifications are reported.
The specific threshold distances are determined by the hardware and the location technologies that are currently available. For example, if Wi-Fi is disabled, region monitoring is significantly less accurate. However, for testing purposes, you can assume that the minimum distance is approximately 200 meters.
Source: https://developer.apple.com/forums/thread/94091
You can assume similar constraints on Android devices.
Geofence.startListening(GeolocationEvent.entry, (entry) {
scheduleNotification("Entry of a georegion", "Welcome to: ${entry.id}");
});
Starts listening for significant location changes on iOS, requests the location every 15 minutes on low power on Android.
Geofence.startListeningForLocationChanges();
Stops listening for significant location changes on iOS, stops requesting the location every 15 minutes on low power on Android.
Geofence.stopListeningForLocationChanges();
This is a stream you can listen to, only triggered by the listening for significant location changes.
Geofence.backgroundLocationUpdated.stream.listen((event) {
scheduleNotification("You moved significantly", "a significant location change just happened.");
});