# Utilize the Smart Geofences API

{% hint style="info" %}
Accessing some of the API classes that are mentioned on this page requires additional Sentiance SDK dependencies. See [this page](https://docs.sentiance.com/important-topics/appendix/on-device-features#smart-geofences) for more information.
{% endhint %}

On this page, you can find examples of how to register for real time smart geofence entry and exit event notifications, how to query the Sentiance SDK for the current smart geofences detection mode, as well as how to refresh the list of monitored smart geofences.

### Listen to smart geofence entry/exit events

{% tabs %}
{% tab title="iOS" %}

```swift
// Create an instance of the delegate that will handle the events.
private let smartGeofenceEventDelegate = MySmartGeofenceEventDelegate()

// Set the delegate on the Sentiance SDK. Note that the SDK holds a weak
// reference to this delegate.
Sentiance.shared.smartGeofenceEventsDelegate = smartGeofenceEventDelegate

// Define the delegate class that will handle the events.
class MySmartGeofenceEventDelegate: SmartGeofenceEventDelegate {
    func onSmartGeofenceEvent(_ smartGeofenceEvent: SmartGeofenceEvent) {
        // Handle the events here.
    }
}
```

{% endtab %}

{% tab title="Android" %}
{% code overflow="wrap" fullWidth="false" %}

```kotlin
import com.sentiance.sdk.smartgeofences.api.SmartGeofenceApi

SmartGeofenceApi.getInstance(mContext).setSmartGeofenceEventListener { event ->
    // Handle the events here.
}
```

{% endcode %}
{% endtab %}

{% tab title="React Native" %}
To get smart geofence entry/exit event updates even when your app is in the background, place the following code inside your app's entrypoint **index.js** file. If you're only interested in these updates when your app is foregrounded, place this code inside the appropriate UI code instead.

```javascript
import {addSmartGeofenceEventListener} from "@sentiance-react-native/smart-geofences";

// If you're subscribing to event updates only in the foreground, make sure
// to call subscription.remove() inside your component's componentWillUnmount() function
const subscription = addSmartGeofenceEventListener(smartGeofenceEvent => {
    // Handle the events here.
});
```

{% endtab %}

{% tab title="Flutter" %}
Create a **background.dart** file under your project's **lib** folder with the following code:

{% code title="background.dart" %}

```dart
import 'package:sentiance_smart_geofences/sentiance_smart_geofences.dart';

@pragma('vm:entry-point')
void registerSmartGeofenceEventListener() async {
  WidgetsFlutterBinding.ensureInitialized();

  SentianceSmartGeofences.registerSmartGeofenceEventListener((smartGeofenceEvent) {
    // Handle the events here.
  });
}
```

{% endcode %}

Add the following code, depending on your target platform.&#x20;

For **iOS**, add the following to your app delegate class:

{% code title="AppDelegate.swift" %}

```swift
import Flutter
import sentiance_smart_geofences

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {

    override func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    ) -> Bool {
    
        // Other code
        
        SentianceSmartGeofencesPlugin.initializeListener(
            withEntryPoint: "registerSmartGeofenceEventListener",
            libraryURI: "package:your_app_package_name/background.dart"
        )
        
        return super.application(application, didFinishLaunchingWithOptions: launchOptions)
    }
}
```

{% endcode %}

For **Android**, add this code to your custom application class:

{% code title="MainApplication.kt" %}

```kotlin
import android.app.Application
import com.sentiance.smart_geofences_plugin.SmartGeofencesPlugin

class MainApplication : Application() {
    override fun onCreate() {
        super.onCreate()
        
        // Other code

        val dartLibrary = "package:your_app_package_name/background.dart"
        SmartGeofencesPlugin.initializeListener(this, dartLibrary, "registerSmartGeofenceEventListener")
    }
}
```

{% endcode %}

If you're calling other APIs from the smart geofences SDK or other 3rd party plugin APIs inside your `registerSmartGeofenceEventListener` Dart function, then you need to register these plugins with the Sentiance SDK. See [this](https://docs.sentiance.com/a-complete-integration/flutter-quick-start/registering-bg-listeners) for more details.
{% endtab %}
{% endtabs %}

### Refresh the list of monitored geofences

The SDK regularly refreshes the list of monitored geofences. You can request an immediate refresh as follows:

{% tabs %}
{% tab title="iOS" %}

```swift
Sentiance.shared.refreshSmartGeofences { result, error in
    if let result {
        print("Geofences refreshed")
    }
    if let error {
        print("Error happened with smart geofence refreshing:" + error.description)
    }
}
```

{% endtab %}

{% tab title="Android" %}
{% code fullWidth="false" %}

```kotlin
import com.sentiance.sdk.smartgeofences.api.SmartGeofenceApi

SmartGeofenceApi.getInstance(mContext).refreshGeofences()
    .addOnSuccessListener {
        Log.d(TAG, "Geofences refreshed")
    }.addOnFailureListener {
        Log.d(TAG, "Failed to refresh geofences. Error: ${it.reason}")
    }
```

{% endcode %}
{% endtab %}

{% tab title="React Native" %}

```javascript
import {refreshGeofences} from "@sentiance-react-native/smart-geofences";

try {
     await refreshGeofences();
     console.log('Geofences refreshed');
} catch (error) {
     const refreshError = error.userInfo;
     console.error('Failed to refresh geofences. Error: ' + refreshError.reason);
}
```

{% endtab %}

{% tab title="Flutter" %}

```dart
import 'package:sentiance_smart_geofences/sentiance_smart_geofences.dart';

final sentianceSmartGeofences = SentianceSmartGeofences();

void refreshGeofences() async {
  String result;

  try {
    await sentianceSmartGeofences.refreshGeofences();
    result = "Geofences refreshed successfully.";
  } on SmartGeofencesRefreshError catch (e) {
    result = "Failed to refresh geofences, reason: ${e.reason.name} - details: ${e.details}";
  } catch (e) {
    result = "An unexpected error occurred: $e";
  }

  print(result);
}
```

{% endtab %}
{% endtabs %}

### Get the current smart geofences detection mode

{% tabs %}
{% tab title="iOS" %}

```swift
let detectionMode = Sentiance.shared.smartGeofenceDetectionMode
print("Detection mode is:" + String(describing: detectionMode))
```

{% endtab %}

{% tab title="Android" %}
{% code fullWidth="false" %}

```kotlin
val detectionMode = SmartGeofenceApi.getInstance(this).detectionMode
Log.d(TAG, "Detection mode is: $detectionMode")
```

{% endcode %}
{% endtab %}

{% tab title="React Native" %}

```javascript
import {getDetectionMode} from "@sentiance-react-native/smart-geofences";

const detectionMode = await getDetectionMode();
console.log('Detection mode is:', detectionMode);
```

{% endtab %}

{% tab title="Flutter" %}

```dart
import 'package:sentiance_smart_geofences/sentiance_smart_geofences.dart';

final sentianceSmartGeofences = SentianceSmartGeofences();

void getDetectionMode() async {
  final detectionMode = await sentianceSmartGeofences.getDetectionMode();
  print('Detection mode is: $detectionMode');
}
```

{% endtab %}
{% endtabs %}
