diff --git a/README.md b/README.md
index 708a01ee..3f1c662e 100644
--- a/README.md
+++ b/README.md
@@ -26,6 +26,7 @@ geofencing:
Also request the correct permissions for geofencing:
```xml
+
```
@@ -42,7 +43,6 @@ class Application : FlutterApplication(), PluginRegistrantCallback {
}
override fun registerWith(registry: PluginRegistry) {
- GeneratedPluginRegistrant.registerWith(registry);
}
}
```
@@ -59,7 +59,6 @@ public class Application extends FlutterApplication implements PluginRegistrantC
@Override
public void registerWith(PluginRegistry registry) {
- GeneratedPluginRegistrant.registerWith(registry);
}
}
```
@@ -105,6 +104,36 @@ And request the correct permissions for geofencing:
```
+Add this line to `Runner-Briding-Header.h`
+
+```h
+#import
+```
+
+At the end add this line to `AppDelegate.swift`
+
+```swift
+GeofencingPlugin.setPluginRegistrantCallback { (registry) in GeneratedPluginRegistrant.register(with: registry) }
+```
+
+### Notes
+Before register geofence request permissions for location and location always. You can use *permission_handler* package. Don't forget include this line in `Podfile`
+
+```ruby
+post_install do |installer|
+ installer.pods_project.targets.each do |target|
+ flutter_additional_ios_build_settings(target)
+ target.build_configurations.each do |config|
+ config.build_settings['ENABLE_BITCODE'] = 'NO'
+ config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= [
+ '$(inherited)',
+ 'PERMISSION_LOCATION=1',
+ ]
+ end
+ end
+end
+```
+
### Need Help?
For help getting started with Flutter, view our online
diff --git a/android/build.gradle b/android/build.gradle
index aa5dced1..e70331fd 100644
--- a/android/build.gradle
+++ b/android/build.gradle
@@ -2,7 +2,7 @@ group 'io.flutter.plugins.geofencing'
version '1.0-SNAPSHOT'
buildscript {
- ext.kotlin_version = '1.5.30'
+ ext.kotlin_version = '1.6.10'
repositories {
google()
@@ -10,7 +10,7 @@ buildscript {
}
dependencies {
- classpath 'com.android.tools.build:gradle:3.5.0'
+ classpath 'com.android.tools.build:gradle:7.1.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
@@ -41,6 +41,6 @@ android {
}
dependencies {
- implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
+ implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation "com.google.android.gms:play-services-location:17.+"
}
diff --git a/android/gradle/wrapper/gradle-wrapper.properties b/android/gradle/wrapper/gradle-wrapper.properties
index 4c83d2a1..cb24abda 100644
--- a/android/gradle/wrapper/gradle-wrapper.properties
+++ b/android/gradle/wrapper/gradle-wrapper.properties
@@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
-distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-all.zip
+distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-all.zip
diff --git a/android/src/main/kotlin/io/flutter/plugins/geofencing/GeofencingPlugin.kt b/android/src/main/kotlin/io/flutter/plugins/geofencing/GeofencingPlugin.kt
index 299b3155..b84f96ba 100644
--- a/android/src/main/kotlin/io/flutter/plugins/geofencing/GeofencingPlugin.kt
+++ b/android/src/main/kotlin/io/flutter/plugins/geofencing/GeofencingPlugin.kt
@@ -257,11 +257,6 @@ class GeofencingPlugin : ActivityAware, FlutterPlugin, MethodCallHandler {
val args = call.arguments>()
when(call.method) {
"GeofencingPlugin.initializeService" -> {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
- mActivity?.requestPermissions(arrayOf(Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_BACKGROUND_LOCATION), 12312)
- } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
- mActivity?.requestPermissions(arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), 12312)
- }
initializeService(mContext!!, args)
result.success(true)
}
diff --git a/example/android/app/build.gradle b/example/android/app/build.gradle
index 74edea83..2cbea396 100644
--- a/example/android/app/build.gradle
+++ b/example/android/app/build.gradle
@@ -26,7 +26,7 @@ apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
android {
- compileSdkVersion 28
+ compileSdkVersion 33
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
@@ -40,7 +40,7 @@ android {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "io.flutter.plugins.geofencing_example"
minSdkVersion 16
- targetSdkVersion 28
+ targetSdkVersion 31
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
@@ -60,7 +60,7 @@ flutter {
}
dependencies {
- implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
+ implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
diff --git a/example/android/app/src/main/AndroidManifest.xml b/example/android/app/src/main/AndroidManifest.xml
index 3425186e..9e4d3863 100644
--- a/example/android/app/src/main/AndroidManifest.xml
+++ b/example/android/app/src/main/AndroidManifest.xml
@@ -15,6 +15,7 @@
+ android:enabled="true" android:exported="true">
diff --git a/example/android/build.gradle b/example/android/build.gradle
index 13546311..31e95773 100644
--- a/example/android/build.gradle
+++ b/example/android/build.gradle
@@ -1,12 +1,12 @@
buildscript {
- ext.kotlin_version = '1.3.50'
+ ext.kotlin_version = '1.6.10'
repositories {
google()
jcenter()
}
dependencies {
- classpath 'com.android.tools.build:gradle:3.6.1'
+ classpath 'com.android.tools.build:gradle:7.1.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
diff --git a/example/android/gradle/wrapper/gradle-wrapper.properties b/example/android/gradle/wrapper/gradle-wrapper.properties
index f0287495..31b394db 100644
--- a/example/android/gradle/wrapper/gradle-wrapper.properties
+++ b/example/android/gradle/wrapper/gradle-wrapper.properties
@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
-distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-all.zip
+distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-all.zip
diff --git a/example/lib/main.dart b/example/lib/main.dart
index 3df1489e..1e831a66 100644
--- a/example/lib/main.dart
+++ b/example/lib/main.dart
@@ -9,6 +9,7 @@ import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:geofencing/geofencing.dart';
+import 'package:permission_handler/permission_handler.dart';
void main() => runApp(MyApp());
@@ -20,28 +21,30 @@ class MyApp extends StatefulWidget {
class _MyAppState extends State {
String geofenceState = 'N/A';
List registeredGeofences = [];
- double latitude = 37.419851;
- double longitude = -122.078818;
- double radius = 150.0;
+ double latitude = 50.00187;
+ double longitude = 36.23866;
+ double radius = 200.0;
ReceivePort port = ReceivePort();
final List triggers = [
GeofenceEvent.enter,
- GeofenceEvent.dwell,
GeofenceEvent.exit
];
final AndroidGeofencingSettings androidSettings = AndroidGeofencingSettings(
- initialTrigger: [
- GeofenceEvent.enter,
- GeofenceEvent.exit,
- GeofenceEvent.dwell
- ],
- loiteringDelay: 1000 * 60);
+ initialTrigger: [
+ GeofenceEvent.enter,
+ GeofenceEvent.exit,
+ ],
+ loiteringDelay: 0,
+ notificationResponsiveness: 0,
+ );
@override
void initState() {
super.initState();
IsolateNameServer.registerPortWithName(
- port.sendPort, 'geofencing_send_port');
+ port.sendPort,
+ 'geofencing_send_port',
+ );
port.listen((dynamic data) {
print('Event: $data');
setState(() {
@@ -51,14 +54,44 @@ class _MyAppState extends State {
initPlatformState();
}
+ void registerGeofence() async {
+ final firstPermission = await Permission.locationWhenInUse.request();
+ final secondPermission = await Permission.locationAlways.request();
+ if (firstPermission.isGranted && secondPermission.isGranted) {
+ await GeofencingManager.registerGeofence(
+ GeofenceRegion(
+ 'mtv',
+ latitude,
+ longitude,
+ radius,
+ triggers,
+ androidSettings,
+ ),
+ callback,
+ );
+ final registeredIds = await GeofencingManager.getRegisteredGeofenceIds();
+ setState(() {
+ registeredGeofences = registeredIds;
+ });
+ }
+ }
+
+ void unregisteGeofence() async {
+ await GeofencingManager.removeGeofenceById('mtv');
+ final registeredIds = await GeofencingManager.getRegisteredGeofenceIds();
+ setState(() {
+ registeredGeofences = registeredIds;
+ });
+ }
+
+ @pragma('vm:entry-point')
static void callback(List ids, Location l, GeofenceEvent e) async {
print('Fences: $ids Location $l Event: $e');
final SendPort send =
- IsolateNameServer.lookupPortByName('geofencing_send_port');
+ IsolateNameServer.lookupPortByName('geofencing_send_port');
send?.send(e.toString());
}
- // Platform messages are asynchronous, so we initialize in an async method.
Future initPlatformState() async {
print('Initializing...');
await GeofencingManager.initialize();
@@ -90,44 +123,16 @@ class _MyAppState extends State {
children: [
Text('Current state: $geofenceState'),
Center(
- child: RaisedButton(
+ child: TextButton(
child: const Text('Register'),
- onPressed: () {
- if (latitude == null) {
- setState(() => latitude = 0.0);
- }
- if (longitude == null) {
- setState(() => longitude = 0.0);
- }
- if (radius == null) {
- setState(() => radius = 0.0);
- }
- GeofencingManager.registerGeofence(
- GeofenceRegion(
- 'mtv', latitude, longitude, radius, triggers,
- androidSettings: androidSettings),
- callback).then((_) {
- GeofencingManager.getRegisteredGeofenceIds().then((value) {
- setState(() {
- registeredGeofences = value;
- });
- });
- });
- },
+ onPressed: registerGeofence,
),
),
Text('Registered Geofences: $registeredGeofences'),
Center(
- child: RaisedButton(
+ child: TextButton(
child: const Text('Unregister'),
- onPressed: () =>
- GeofencingManager.removeGeofenceById('mtv').then((_) {
- GeofencingManager.getRegisteredGeofenceIds().then((value){
- setState(() {
- registeredGeofences = value;
- });
- });
- }),
+ onPressed: unregisteGeofence,
),
),
TextField(
@@ -136,17 +141,17 @@ class _MyAppState extends State {
),
keyboardType: TextInputType.number,
controller:
- TextEditingController(text: latitude.toString()),
+ TextEditingController(text: latitude.toString()),
onChanged: (String s) {
latitude = double.tryParse(s);
},
),
TextField(
decoration:
- const InputDecoration(hintText: 'Longitude'),
+ const InputDecoration(hintText: 'Longitude'),
keyboardType: TextInputType.number,
controller:
- TextEditingController(text: longitude.toString()),
+ TextEditingController(text: longitude.toString()),
onChanged: (String s) {
longitude = double.tryParse(s);
}),
@@ -154,11 +159,11 @@ class _MyAppState extends State {
decoration: const InputDecoration(hintText: 'Radius'),
keyboardType: TextInputType.number,
controller:
- TextEditingController(text: radius.toString()),
+ TextEditingController(text: radius.toString()),
onChanged: (String s) {
radius = double.tryParse(s);
}),
]))),
);
}
-}
\ No newline at end of file
+}
diff --git a/example/pubspec.yaml b/example/pubspec.yaml
index f0512163..f41adf44 100644
--- a/example/pubspec.yaml
+++ b/example/pubspec.yaml
@@ -12,6 +12,7 @@ dependencies:
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.2
+ permission_handler: ^10.2.0
dev_dependencies:
flutter_test:
diff --git a/ios/Classes/GeofencingPlugin.m b/ios/Classes/GeofencingPlugin.m
index 130dd739..6772de6d 100644
--- a/ios/Classes/GeofencingPlugin.m
+++ b/ios/Classes/GeofencingPlugin.m
@@ -143,8 +143,8 @@ - (instancetype)init:(NSObject *)registrar {
_eventQueue = [[NSMutableArray alloc] init];
_locationManager = [[CLLocationManager alloc] init];
[_locationManager setDelegate:self];
- [_locationManager requestAlwaysAuthorization];
- _locationManager.allowsBackgroundLocationUpdates = YES;
+ // [_locationManager requestAlwaysAuthorization];
+ // _locationManager.allowsBackgroundLocationUpdates = YES;
_headlessRunner = [[FlutterEngine alloc] initWithName:@"GeofencingIsolate" project:nil allowHeadlessExecution:YES];
_registrar = registrar;
diff --git a/lib/src/callback_dispatcher.dart b/lib/src/callback_dispatcher.dart
index fe1e0bad..12d88798 100644
--- a/lib/src/callback_dispatcher.dart
+++ b/lib/src/callback_dispatcher.dart
@@ -10,6 +10,7 @@ import 'package:flutter/services.dart';
import 'package:geofencing/src/geofencing.dart';
import 'package:geofencing/src/location.dart';
+@pragma('vm:entry-point')
void callbackDispatcher() {
const MethodChannel _backgroundChannel =
MethodChannel('plugins.flutter.io/geofencing_plugin_background');
@@ -17,7 +18,7 @@ void callbackDispatcher() {
_backgroundChannel.setMethodCallHandler((MethodCall call) async {
final List args = call.arguments;
- final Function callback = PluginUtilities.getCallbackFromHandle(
+ final Function? callback = PluginUtilities.getCallbackFromHandle(
CallbackHandle.fromRawHandle(args[0]));
assert(callback != null);
final List triggeringGeofences = args[1].cast();
@@ -29,7 +30,7 @@ void callbackDispatcher() {
.forEach((dynamic e) => locationList.add(double.parse(e.toString())));
final Location triggeringLocation = locationFromList(locationList);
final GeofenceEvent event = intToGeofenceEvent(args[3]);
- callback(triggeringGeofences, triggeringLocation, event);
+ callback?.call(triggeringGeofences, triggeringLocation, event);
});
_backgroundChannel.invokeMethod('GeofencingService.initialized');
}
diff --git a/lib/src/geofencing.dart b/lib/src/geofencing.dart
index be09b06b..844a6425 100644
--- a/lib/src/geofencing.dart
+++ b/lib/src/geofencing.dart
@@ -72,10 +72,13 @@ class GeofenceRegion {
final AndroidGeofencingSettings androidSettings;
GeofenceRegion(
- this.id, double latitude, double longitude, this.radius, this.triggers,
- {AndroidGeofencingSettings androidSettings})
- : location = Location(latitude, longitude),
- androidSettings = (androidSettings ?? AndroidGeofencingSettings());
+ this.id,
+ double latitude,
+ double longitude,
+ this.radius,
+ this.triggers,
+ this.androidSettings,
+ ) : location = Location(latitude, longitude);
List _toArgs() {
final int triggerMask = triggers.fold(
@@ -102,10 +105,12 @@ class GeofencingManager {
/// Initialize the plugin and request relevant permissions from the user.
static Future initialize() async {
- final CallbackHandle callback =
+ final CallbackHandle? callback =
PluginUtilities.getCallbackHandle(callbackDispatcher);
- await _channel.invokeMethod('GeofencingPlugin.initializeService',
- [callback.toRawHandle()]);
+ if (callback != null) {
+ await _channel.invokeMethod('GeofencingPlugin.initializeService',
+ [callback.toRawHandle()]);
+ }
}
/// Promote the geofencing service to a foreground service.
@@ -142,7 +147,7 @@ class GeofencingManager {
throw UnsupportedError("iOS does not support 'GeofenceEvent.dwell'");
}
final List args = [
- PluginUtilities.getCallbackHandle(callback).toRawHandle()
+ PluginUtilities.getCallbackHandle(callback)!.toRawHandle()
];
args.addAll(region._toArgs());
await _channel.invokeMethod('GeofencingPlugin.registerGeofence', args);
diff --git a/pubspec.yaml b/pubspec.yaml
index e4fa136d..b3a5ad08 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -1,12 +1,11 @@
name: geofencing
description: A geofencing plugin for Flutter applications.
version: 0.1.0
-author: Ben Konyi
homepage: https://github.com/flutter/plugins
environment:
- sdk: ">=2.1.0 <3.0.0"
- flutter: "^1.12.0"
+ sdk: ">=2.12.0 <3.0.0"
+ flutter: "^2.0.0"
dependencies:
flutter: