Skip to content

Geofence

Introduction

StreetHawk Geofence is used for running location-based campaigns based on user's entry and exit into a geofence area. The document describes integrating StreetHawk SDK's Geofence module into your application. You will be required to register the geofence of interest into StreetHawk console. Steps for registering geofence in StreetHawk console are described below under Create Your First Geofence Based Campaign section.

Pre-requisites

The documents assume that you have downloaded and added the boiler-plate code described in the introduction document. You will require IStreetHawkGeofence.cs, StreetHawkGeofemce.cs (iOS) and StreetHawkGeofence.cs (Android) in particular for StreetHawkGrowth component.

StreetHawk Geofence component depends on StreetHawk Analytics components. This document assumes that you have already integrated StreetHawk Analytics. If not then click here for detailed documentation which describes steps to integrate Analytics component.

Integration Steps

  • Add StreetHawk Growth component
  • Add StreetHawk Growth component in both iOS and Droid project as shown below:

  1. In iOS project, right click on component folder
  2. Click on "Get More Components" and search for StreetHawk Growth. Click Add to App.
  3. Once downloaded, click on Add to Project.
  4. Repeat the above steps for adding the component in .Droid project of your application

  • Add platform specific code
    • iOS
      Modify Info.plist to enable location service.
  • Add NSLocationAlwaysUsageDescription: request “Always” permission. (Option available in iOS 8). This is needed when you use location functions, including "streethawk/Locations" or "streethawk/Geofences" or "streethawk/Beacons".

    • Android
      Copy and pate the below code in AndroidManifest.xml located under properties folder of your application's .Droid project
      <manifest">
      
          <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
      
          <application>
              <uses-library android:name="com.google.android.maps" />
      
              <service
                  android:name="com.streethawk.library.geofence.GeofenceService"
                  android:exported="false"></service>
              <receiver
                  android:name="com.streethawk.library.geofence.SHCoreModuleReceiver"
                  android:enabled="true"
                  android:exported="true">
                  <intent-filter>
                      <action android:name="com.streethawk.intent.action.APP_STATUS_NOTIFICATION" />
                      <action android:name="android.location.PROVIDERS_CHANGED" />
                  </intent-filter>
              </receiver>
      
              <activity android:name="com.streethawk.library.geofence.AskGeoPermission" />
          </application>
      </manifest> 
      
  • Start Monitoring for geofences

Start monitoring for geofences by calling StartGeofenceMonitoring method as shown below.

DependencyService.Get<IStreetHawkGeofence>().StartGeofenceMonitoring();

Create Your First Geofence Based Campaign

  • Create Geofence in StreetHawk Console

Open StreetHawk console and click on Geofence&Beacons under Growth Campaigns as shown below. Enter Title of the geofence and place the thumb pin at desired location to autofill latitude and longitude of your geofence.

  • Select Geofence filter

Create a new campaign by selecting Campaigns under Growth Campaigns and by clicking the add button. Click on Geofence Filter as shown below. You may opt to select some single geofence or all geofences by clicking on the locations drop down and selecting the geofences for campaigns. You can select the minimum distance for triggering by selecting distance from around drop down menu.

  • Select an action

Proceed to Choose the Engagement Method and select the desired action to be triggered when application user enters the geofence.

  • Set campaign running duration

Schedule your campaign to run over the period of time desired by selecting Real-Time Monitoring option. Please note if the option is not selected, your campaign will fire only once and will target only those users who are present in the geofence at the moment when campaign ran.

  • Save and run campaign

Give a title to your campaign and launch the campaign.

  • Get notified when enters and exits a Geofence

It's not mandatory but if you would like to get notified when Geofence enter or exit, you can add the callback function.

Add observer and get noticed when enter or exit a server monitoring geofence. The callback is an object of the geofence information.

using StreetHawkCrossplatform;

protected override async void OnStart()
{
......
DependencyService.Get<IStreetHawkGeofence>().RegisterForGeofenceStatus( delegate (SHGeofenceObj geofence)
{
Device.BeginInvokeOnMainThread(() =>
{
string message = string.Format("latitude: {0}, longitude: {1}, radius: {2}, server id: {3}, inside: {4}.", geofence.latitude, geofence.longitude, geofence.radius, geofence.serverId, geofence.isInside);
MainPage.DisplayAlert("Enter/Exit geofence: ", message, "OK");
});
});
  • Delay asking for Permission

By default StreetHawk SDK requests location permission when function “Init” called in the “OnStart”. In case you may wish to delay asking the user for location permission, you can add the code be as shown below.

using StreetHawkCrossplatform;

//optional to delay ask for permission
DependencyService.Get<IStreetHawkGeofence>().SetIsDefaultLocationServiceEnabled(false);  //make streethawkinit not ask for location permission, delay for later time.

//do DependencyService.Get<IStreetHawkAnalytics>().Init(); and other things.

DependencyService.Get<IStreetHawkGeofence>().SetIsLocationServiceEnabled(true);  //later when ready to register for location service, call this.