Utilize the Smart Geofences API
Accessing some of the API classes that are mentioned on this page requires additional Sentiance SDK dependencies. See this page for more information.
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
// 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.
}
}
import com.sentiance.sdk.smartgeofences.api.SmartGeofenceApi
SmartGeofenceApi.getInstance(mContext).setSmartGeofenceEventListener { event ->
// Handle the events here.
}
import {addSmartGeofenceEventListener} from "@sentiance-react-native/smart-geofences";
const subscription = addSmartGeofenceEventListener(smartGeofenceEvent => {
// Handle the events here.
});
Create a background.dart file under your project's lib folder with the following code:
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.
});
}
Add the following code, depending on your target platform.
For iOS, add the following to your app delegate class:
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)
}
}
For Android, add this code to your custom application class:
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")
}
}
Refresh the list of monitored geofences
The SDK regularly refreshes the list of monitored geofences. You can request an immediate refresh as follows:
Sentiance.shared.refreshSmartGeofences { result, error in
if let result {
print("Geofences refreshed")
}
if let error {
print("Error happened with smart geofence refreshing:" + error.description)
}
}
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}")
}
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);
}
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);
}
Get the current smart geofences detection mode
let detectionMode = Sentiance.shared.smartGeofenceDetectionMode
print("Detection mode is:" + String(describing: detectionMode))
val detectionMode = SmartGeofenceApi.getInstance(this).detectionMode
Log.d(TAG, "Detection mode is: $detectionMode")
import {getDetectionMode} from "@sentiance-react-native/smart-geofences";
const detectionMode = await getDetectionMode();
console.log('Detection mode is:', detectionMode);
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');
}
Last updated