Skip to main content

Swrve Geo

Send targeted, geo-triggered notifications to your app users with Swrve Geo. To use this feature, you must integrate the Geo project alongside the general Swrve SDK for your platform. This guide explains how to integrate Swrve Geo for iOS, Android, and React Native.


Requirements​

  • Request a copy of the Swrve Geo project for your app platform from your CSM at support@messagegears.com. They will enable Geoplaces in your Swrve dashboard.
  • The current version of Swrve Geo has the following native SDK / Plugin minimum version dependency:
    • iOS SDK 7.0.3
    • Android SDK 11.0.0
    • React Native SDK plugin 1.0.0
  • Swrve Geo for Android uses Android's native geofencing APIs, so the following services and permissions are required:
    • Google Play Services
    • Wi-fi scanning set to "on"
    • Location setting set to "on"
    • On Android API level 26 and above, a foreground notification is recommended for reliable geoplace triggering. While the Swrve SDK can function without it, omitting the foreground notification will reduce the accuracy and timeliness of geoplace triggers. You can optionally configure this notification within SwrveGeoConfig.
    • For Android API level 28 and lower, use the ACCESS_FINE_LOCATION permission.
    • For Android API level 29, the ACCESS_BACKGROUND_LOCATION permission must be set to "Allow all the time". The β€œAllow only while using the app” permission is not supported.
  • Swrve Geo for iOS requires iOS 15.0 or later. It uses iOS’s native region monitoring and, unless you turn on reduced location usage, the Significant-Change Location service. The following services and permissions are required:
    • Location Services set to "On"
    • Location permission set to "Always". With "While Using", iOS holds geofence events until the user next opens your app, and SwrveGeoSDK discards them as out of date.
    • Precise Location set to "On". With approximate location, region monitoring doesn't work, so enter and exit transitions don't trigger.
  • On every platform, the user must allow notifications for your app, because geo-triggered campaigns are notifications. Ask for notification permission before you start SwrveGeoSDK.
  • Once integrated, SwrveGeoSDK silently does nothing on devices that don’t have the appropriate device capabilities. Both iOS and Android platforms can restrict installations of an app to devices that only support these capabilities. If you’d like to restrict your app to be only installed on those devices, speak to your CSM.

Integration instructions​

Select one of the platform tabs below for integration instructions.

There are two main folders in the Geo project for Android:

  • SwrveGeoSDK is an Android library project that you integrate as a separate Gradle dependency alongside the standard Swrve Android SDK.
  • SwrveGeoSample provides a basic example of how to integrate Swrve Geo for Android.

Integration steps​

Step 1: Add the following dependencies to your app build.gradle file:

api 'com.swrve.sdk.android:swrve-geo:7.3.0'

Step 2: Choose when and where to ask for location permission. You also have the option to include a pre-prompt and post-prompt dialog.

  • Call the SwrveGeoSDK Start API at the moment you want to display the location permission prompt. Further calls to this API will have no effect unless you call the SwrveGeoSDK Stop API.
SwrveGeoSDK.start(this);

Note: If your native SDK configuration uses MANAGED mode for creating the SwrveSDK instance, then start the SwrveGeoSDK only after you've called the SwrveSDK Start API.

  • Android best practices recommend explaining to your users why your app requires location permission before requesting it. For example,

Example of an Android preprompt that explains why your app needs access to the user location

To display your explanation and prompt buttons before you initiate the system permission request, use the permissionPrePrompt API in the SwrveGeoConfig. If you prefer to use your own UI or don't want to include a pre-prompt, skip this option.

SwrveGeoConfig geoConfig = new SwrveGeoConfig.Builder()
.permissionPrePrompt(getString(R.string.pre_prompt_rationale),
getString(R.string.pre_prompt_proceed_button_text),
getString(R.string.pre_prompt_cancel_button_text))
.build();

SwrveGeoSDK.init(this, geoConfig);
  • If a customer declines the system permission prompt, use the permissionDeniedPostPrompt API to confirm their selection and explain the features they miss out on by not giving permission. For example,

Example prompt that explains to customers the features they miss if they do not grant location permissions.

This option also gives you the opportunity to include a button that takes the user to the system settings if they want to manually grant permission. If you don't want to display the additional prompt, skip this option.

SwrveGeoConfig geoConfig = new SwrveGeoConfig.Builder()
.permissionDeniedPostPrompt(getString(R.string.post_prompt_text),
getString(R.string.post_prompt_settings_button_text),
getString(R.string.post_prompt_cancel_button_text))
.build();

SwrveGeoSDK.init(this, geoConfig);

Step 3: In the Application class where the SwrveSDK instance is created, initialize SwrveGeoSDK directly after you initialize SwrveSDK.

For better performance on devices running Android Oreo and above, we also recommend setting the foreground service notification and notification ID in the SwrveGeoConfig. The notification ID is a unique integer you must set that identifies each notification. Providing value in the content of the foreground service notification makes it easier for users to buy in to allowing this. To keep the content relevant, use the same notification ID to update the notification content on a regular basis.

Replace <app_id> and <api_key> with your app ID and API key.

SwrveSDK.createInstance(application, <app_id>, <api_key>);

Notification fgNotification = getGeoForegroundNotification(); // implement your own foreground notification
SwrveGeoConfig geoConfig = new SwrveGeoConfig.Builder()
.foregroundNotification(fgNotification, YOUR_FOREGROUND_NOTIFICATION_ID)
.permissionPrePrompt(getString(R.string.pre_prompt_rationale),
getString(R.string.pre_prompt_proceed_button_text),
getString(R.string.pre_prompt_cancel_button_text))
.permissionDeniedPostPrompt(getString(R.string.post_prompt_text),
getString(R.string.post_prompt_settings_button_text),
getString(R.string.post_prompt_cancel_button_text))
.build();
SwrveGeoSDK.init(this, geoConfig);

Step 4: If your app configures the foreground notification in SwrveGeoConfig, you must also declare the use of the FOREGROUND_SERVICE_LOCATION permission and provide a justification for its use in the Google Play Console. This is required by Google Play's policy for apps using foreground services related to location. Add the following permission and service in your AndroidManifest.xml.

<uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION" />
<application>
....
<service
android:name="com.swrve.sdk.geo.SwrveGeoForegroundLocationUpdatesService"
android:enabled="true"
android:exported="false"
android:foregroundServiceType="location" />
.....
</application>

Step 5: Optionally, to update the foreground notification with more relevant content, use the same notification ID you set above to update the existing notification content.

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, "123");
mBuilder.setSmallIcon(R.drawable.logo)
.setContentText("Updated message that provides value")
.setContentTitle("Title");
Notification notification = mBuilder.build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(YOUR_FOREGROUND_NOTIFICATION_ID, notification);

Note: On every platform, SwrveGeoSDK saves whether it's started for each user. If your app identifies users, start SwrveGeoSDK once identify succeeds, for example in its success callback, and again each time you identify a different user.


Location permission on iOS​

Swrve Geo triggers in the background only when the user allows location Always. iOS doesn't offer Always in the first location prompt, so getting Always takes two prompts:

  1. The first prompt offers Allow Once, Allow While Using App, and Don't Allow.
  2. Once the user allows While Using, the app asks for Always, and iOS shows the upgrade prompt, which offers Change to Always Allow and Keep Only While Using.

iOS shows the upgrade prompt only once. After the user declines it, only the Settings app can turn on Always.

Choose how SwrveGeoSDK asks​

To choose what the SwrveGeoSDK Start API asks for, set permissionRequestMode in SwrveGeoConfig:

  • whenInUseThenAlways - The default. SwrveGeoSDK asks for While Using, then for Always as soon as it's allowed, so iOS shows the upgrade prompt while the user is still in your app.
  • always - SwrveGeoSDK asks for Always in one request, as in versions before 5.2.0. If the user allows While Using, iOS shows the upgrade prompt later, the first time your app uses location in the background, over whatever the user is doing at the time.
  • never - SwrveGeoSDK never asks, so your app chooses the moment and explains why. Ask for While Using, then Always. You still call the Start API, before or after asking, and SwrveGeoSDK sets up geofences as soon as either is allowed.
let geoConfig = SwrveGeoConfig()
geoConfig.permissionRequestMode = .never
SwrveGeoSDK.initWith(geoConfig)

If the user selects Don't Allow and later allows location in the Settings app, SwrveGeoSDK picks it up without another call to the Start API.

Ask with an in-app message​

Ask at a moment when location helps the user, such as after they place a pickup order, rather than when your app first opens. To ask with the default permissionRequestMode:

  1. Show an in-app message that explains what the user gets, and that iOS asks twice.
  2. On its button, use the Start Geo SDK action. SwrveGeoSDK starts and asks for permission.
  3. For users who didn't allow Always, show a second in-app message that uses the Open app settings action, so they can turn on Always in the Settings app. To target these users, use the Swrve.permission.ios.location.always device property, which SwrveGeoSDK sets to authorized only for Always.

The Start Geo SDK action needs no extra code in your app. The SDK shows the message only while SwrveGeoSDK isn't started. With permissionRequestMode set to never, use the Request Permission action for location instead, which needs your app to implement SwrveInAppCapabilitiesDelegate. It only asks for permission, so your app still starts SwrveGeoSDK. For more information, see iOS.

Keep the message consistent with the location descriptions in your Info.plist, because iOS shows those in the prompts straight after. For more information, see Editing in-app message content.


Reduce location use on iOS​

By default, while it's started, SwrveGeoSDK monitors significant location changes, so iOS wakes your app with a location about every 500 meters the user travels. SwrveGeoSDK also gets a location each time a session starts, so iOS shows the location indicator when your app opens. iOS also reminds users from time to time which apps use their location in the background.

To use location less often, set reducedLocationUsage to true. SwrveGeoSDK then:

  • Relies on geofences alone, so iOS wakes your app only when the user enters or leaves one.
  • Doesn't get a location each time your app opens. iOS still briefly uses location when SwrveGeoSDK sets up new geofences, for example after your geoplaces change.

The trade-off is that changes to your geoplaces reach users later. SwrveGeoSDK checks for them about once a day, and straight away when the user leaves the area your geofences cover:

  • Default - When your app opens, the user enters or leaves a geofence, or the user travels about 500 meters.
  • With reducedLocationUsage - When your app opens, around the location SwrveGeoSDK last used, or when the user enters or leaves a geofence.

Geo campaigns aren't affected. SwrveGeoSDK refreshes them each time your app opens.

let geoConfig = SwrveGeoConfig()
geoConfig.reducedLocationUsage = true
SwrveGeoSDK.initWith(geoConfig)

Custom filtering​

To filter notifications or modify their content based on custom properties, you can set a custom filter when initializing SwrveGeoSDK.

SwrveGeoConfig geoConfig = new SwrveGeoConfig.Builder()
.customFilter((builder, id, properties) -> {
// add custom modifications if necessary or return null to suppress it
return builder.build();
})
.build();

// Initialize the SDK
SwrveGeoSDK.init(geoConfig);

Geoplace transition callbacks​

When a customer enters or exits a geoplace, you might want to execute your own additional logic via geoplace transition callbacks. Configure the relevant platform callback as part of SwrveGeoConfig.

Implement the SwrveGeofenceTransitionListener callback.

SwrveGeoConfig geoConfig = new SwrveGeoConfig.Builder()
.geofenceTransitionListener((name, transition, triggerLocation, customProperties) -> {
// add custom code to execute upon enter/exit a geofence.
// Note this executes in same BroadcastReciever as Swrve code so has limitations.
return builder.build();
})
.build();

SwrveGeoSDK.init(this, geoConfig);

Note: Be aware that the SwrveGeofenceTransitionListener code runs in the same BroadcastReceiver as Swrve code. Create a separate worker thread from this to execute your own custom code.


Testing your integration​

It's not always possible or practical to manually QA test geo-triggered campaigns, so in some instances you might want to use a simulator or emulator to test moving through a geographic location. This section explains how to trigger geoplaces from Android Studio and Xcode.

Swrve Geo for Android is optimized to run in a battery-friendly manner, thus it doesn't use the GPS provider. The Android emulator runs only with GPS provider, so using the emulator location tools to test Swrve geoplace triggers requires a few extra steps. There are several methods to achieve this, but the easiest way is to allow the Google Maps app to get a location update and SwrveGeoSDK will passively use it in the background. The instruction below explain how to do this.

Preliminary setup​

To test geoplace triggers in Android studio, first create an emulator and then add geoplaces in Swrve. Note: Some of the settings or locations referenced below may differ depending on the Android OS version you're using.

  • Step 1: In Android Studio, open the Android SDK Manager and make sure SDK tools are up to date.
  • Step 2: Open the Android Virtual Device (AVD) Manager and create your emulator Virtual Device with hardware that includes the Play Store (Google Play services).
  • Step 3: Start the emulator and find the Security or Location settings.
  • Step 4: Ensure the Location setting is on. Depending on the OS version, this might be located under Privacy.
  • Step 5: Ensure Google Location Accuracy is in High Accuracy mode or Improve Location Accuracy is on. Depending on the OS version this is either under Location Mode, Advanced settings, or Improve Location Accuracy. This also requires accepting the Google location service policy when prompted.
  • Step 6: If you're using an older emulator, you might need to update to Google Play Services. To do this, add a Google account to the emulator, open the Play store, and update all apps.
  • Step 7: On the emulator, open Google maps to view the current location.
  • Step 8: To change the location, open the emulator extended controls. On the Location tab, there's a map and button for setting the location. Set the desired location and verify Google Maps updates to the selected location.
  • Step 9: In Swrve, add geoplaces for the locations you want to test. For information about creating geoplaces, see About geoplaces.

Testing geoplace triggers​

  • Step 1: In your Swrve SDK configuration, set the Logcat logging level to DEBUG. For more information, see How do I configure SDK device logging?
  • Step 2: Install your app on the emulator and set it up as a QA device in Swrve. For information on setting up QA devices, see How do I set up QA devices?
  • Step 3: Start SwrveGeoSDK and verify there's no geofencing errors in the Logcat console.
    • If a message displays saying Google Play services are out-of-date, open the Play store and update all apps. Then restart your app.
    • If Logcat displays ApiException 1000, it might indicate that Location services are not on, location accuracy is not in High Accuracy mode, or Improve Location Accuracy is not on.
  • Step 4: Open Google Maps.
  • Step 5: Open the emulator extended controls. On the Location tab there's a map and button for setting the location. Set the location to the center of a geoplace and debug logs will appear in Logcat about the trigger.
  • Step 6: In Swrve, check the QA device logs. The QA User Logging screen should display information about the triggered geoplace. If the trigger was suppressed, the Geoplace Triggered event will include the reason.

Next steps​