Last modified September 4, 2020 by Shelly Wolfe


Swrve is a single integrated platform delivering everything you need to drive mobile engagement and create valuable consumer relationships on mobile. The Swrve native Android SDK enables your app to use all of these features. This guide contains all the information you need to integrate the Swrve SDK into your app.
The code examples in this guide refer exclusively to Swrve Android SDK version 7.1.0+. If you are upgrading from an SDK older than version 7.1.0, please refer to the SDK release notes to review major changes made to the SDK methods and APIs in all major versions.


  • The Swrve Android SDK supports Android 4.1 and later (API level 16 and above) but is able to handle older versions with an empty SDK that does nothing.
  • OTT – If you are compiling your app for Android TV or Amazon Fire TV, there are no additional integration steps, however you must set the Android SDK to API level 21 (the minimum required to run Android TV). For more information on the features and campaign types supported in each OTT platform, see the OTT landing page.
  • Gradle is distributed with the SDK and used to build the SDK and its dependencies.
  • Ensure you have the latest version of Android Studio.
  • As Google have deprecated Google Cloud Messaging (GCM), we recommend upgrading to Firebase Cloud Messaging (FCM). Note: We have completely removed support for GCM as of version 6.0 of our Android SDK.
  • The App ID and API Key for your app. This information is available in Swrve on the Integration Settings screen (on the Settings menu, select Integration settings).

Installing the SDK

Swrve has an open source SDK repository. There are two options for downloading the latest public Swrve Android SDK:

  • Install the Android SDK libraries using Gradle from the Swrve repository on Jcenter.
  • Download the SDK from the GitHub public repository.

Install using Gradle

To install the Android SDK library in your project, add the following code to your build.gradle file.

Step 1: Add the Swrve repository and the Google repository.

Step 2: Choose a Swrve library to install, ensuring you include the latest SDK version number (for example, 7.3.0). If you plan to integrate push notifications, use the Firebase or Amazon-flavored libraries. Otherwise, use the basic Android SDK.

Firebase – select if you plan to use push notifications with Swrve, or already use any Firebase services like Firebase Cloud Messaging or Analytics.

Amazon – select if you’re using Amazon Device Messaging (ADM).


If you’re moving from an earlier version of the Android SDK to the current version, see the Android SDK Upgrade Guide for upgrade instructions.

Amazon SDK dependency

If you’re compiling the Amazon-flavored SDK for Android from source, it is necessary to also download the Amazon SDK from the Amazon Developer portal to get the ADM jar support file (amazon-device-messaging-1.1.0.jar). Add the ADM jar file to your project in the appropriate folder. If you are using the Swrve Gradle build configurations, the location is ./SwrveSDK/providedLibs/.

The corresponding project build.gradle files refer to the jar file using the “provided” directive that when combined with the Amazon flavor SDK, becomes amazonProvided as follows:

The providing directive tells the library or plugin to compile against the jar but not include the class implementations. They are simply stub implementations and will fail if actually executed. The runtimes exist on the Amazon device.

Debug applications (apk) built using Gradle and being built from source may also need to make use of the amazonProvided directive pointing to the adm jar in their own build.gradle dependency section. This isn’t required for release.

Initializing the SDK

Depending on your data requirements, Swrve stores all customer data and content in either our US or EU data centers. If your app uses EU data storage and URL endpoints (that is, you log into the Swrve dashboard at, include the EU stack information in the example below. If you have any questions or need assistance configuring the SDK for EU data storage, please contact

To initialize the SDK, create an instance on your application level. Replace <app_id> and <api_key> with your app ID and API key.

Minimum Android API level

The minimum API level that the Swrve SDK supports is level 16. If your app supports below level 16, the Swrve SDK will not track or execute on those earlier versions, but will operate as normal on level 16 and above.

If you are compiling your app for Android TV or Amazon Fire TV, you must set the Android API level to 21 (the minimum required to run Android TV).

If your app supports API levels below 16, you need to override the Swrve library defaults. Include the following overrides in your AndroidManifest.xml:

By default, the Swrve Android SDK enables you to track important metrics and run in-app message and Conversations campaigns. To make use of additional Swrve features, such as user identity, push notifications, and advanced metrics tracking, complete the following sections as required.

Advanced initialization mode

As of Android SDK version 6.3, there are two modes for initializing the SDK: AUTO and MANAGED. In AUTO mode, the SDK is automatically initialized as outlined in the previous section. However, if you want to delay the point at which the SDK starts tracking and sending events for a particular user, set the initMode property in the SwrveConfig to MANAGED.  For example, you might want to use a custom ID as the Swrve user ID and not start sending events or targeting a customer until they’ve confirmed their user ID (that is, they’ve logged in). If you use MANAGED mode, the SDK will delay tracking by default until the user ID is set at least once via the start API.

Do not use email or other personally identifiable information (PIIs) for the custom user ID, as this could be used to compromise your app or integration. Instead, use an internal ID that is only known to your CRM and cannot be linked back to a user outside of your system.

If you want to delay starting the SDK every time the SDK is initialized (even if you’ve started it in a previous session), then set the managedModeAutoStartLastUser configuration property to false.

Constraints and considerations

Depending on the initialization mode you choose to use, there are certain limits or nuances you should be aware of:

  • In MANAGED mode, you can’t call the Identify API. If you try to call the Identify API, it will result in a fail fast exception.
  • In AUTO (default) mode, you can’t call the following APIs. If you try to call them, it will result in a fail fast exception:
    • start()
    • start(userId)
  • In-app campaigns linked to a push notification are blocked in configurations where:
    • initMode is MANAGED
    • managedModeAutoStartLastUser is false
  • Push notification engaged events are always tracked against the last known user ID (even if initMode is MANAGED and managedModeAutoStartLastUser is false).
  • Use the SwrveSDK.isStarted() API to check if the SDK is started before calling regular APIs when initMode is MANAGED and managedModeAutoStartLastUser is false.

User identity

Swrve supports an Identify API to find preexisting user identities that an app has logged either on a single device or multiple devices. You can now identify users and then track and target them safely across multiple devices, platforms, and channels. For more information, see Tracking your users with Swrve User Identity.

The external ID is a non-discoverable key that identifies your user across multiple channels and platforms. Emails or other personally identifiable information (PIIs) are not accepted as external user IDs, they will be rejected on the server side. Before you implement the Identity service, please consult with your Customer Success Manager at for some guidance on best practices to follow in your integration.


Initialize SwrveSDK as normal in your application:

Do not call Identify from the Application class.

When you are ready, call the Identity API with your external user ID:

If the call fails, then the user will not be correctly linked on the Swrve backend system, which may affect reporting and audiences where the user is logging in on multiple devices. We recommend not queuing or sending events until the identify call completes.

Push notifications

Use Swrve’s push notification campaigns to send personalized messages to your app users while they’re outside of your app or to send silent background app updates. For more information, see Intro to push notifications.

Push notifications are disabled by default. To enable push notifications, use either the Firebase- or Amazon-flavored SDK.

Push notification campaigns are not currently supported on OTT platforms.

Using Swrve push notifications with other providers

For information about how to integrate Swrve push notifications with other providers, see the Multiple FCM Providers example in the Android SDK samples folder on GitHub.

Using only Swrve push notifications

If this is the first push notification integration for your app, complete the following steps.

Step 1: Make the following changes to your AndroidManifest.xml to include the FCM or ADM receiver, Swrve’s intent service, permissions and configuration metadata.


Step 2: Add the following script dependency:

Step 3: Add the following plugin to the build.gradle file of your app.

Step 4: Add the google-services.json from the Firebase Console to your project. For more information, see the Firebase help article, Download a configuration file.

Step 5: Provide your notification configuration to the SwrveConfig and pass into SwrveSDK.createInstance.



  • For more information, see Amazon Device Messaging.
  • Add the the Amazon namespace xmlns:amazon=” to your <manifest>.

Step 2: If you do your own application signing, you need to add the Amazon api_key.txt to the assets folder of your project. To get this key from Amazon, follow this guide on Obtaining Amazon Device Messaging Credentials.

Step 3: Provide your notification configuration to the SwrveConfig and pass into SwrveSDK.createInstance.

Configuring silent notifications

To enable silent push notifications in your app, add the new silent push listener in your Application’s onCreate activity:

The purpose of a silent notification is to deliver data to your app in the background, such as a content update. You should never call the SDK from a silent notification.

Note: This listener is executed from the push service.

Creating and entering the Android server key

To enable your app to send push notifications to Google Play devices, you require a server key. For more information, see How do I manage the server key for push notifications?

Enabling Amazon Device Messaging

To enable ADM in your Amazon app, log in to the Amazon Developer portal, go to Apps & Services > My App > Device Messaging and select Enable.

Creating and entering the Amazon Client ID and Client Secret

To enable your app to send push notifications to Amazon devices, you need the Client ID and Client Secret for your app. For information on how to get these credentials, see Obtaining Amazon Device Messaging Credentials.

Generally, you enter the Client Id and Client Secret on the Swrve dashboard when configuring the Integration Settings as part of the Swrve onboarding process. You can edit the settings on this screen later on if required.

Step 1: On the Settings menu, select Integration Settings.

Step 2: Under the Push Notifications section, in the Amazon Device Messaging Service section, enter your Client Id and Client secret.

Step 3: Test the server key by sending a test push notification to one of your QA devices. To send a test push notification:

  • Select your QA device from the list of available devices.
  • Select Send Test Push.

Advanced push notification configuration

This section describes how to configure advanced options for Android SDK push notification integration.

Processing custom payloads

To execute code when a notification is opened, add a listener to you config as part of initialization in your Application class:

Using custom sounds

You can send push notifications with custom sounds. To do so, place your custom sound under res/raw and set your sounds in the Swrve service. For more information about adding custom sounds in Swrve, see Intro to push notifications.

In-app messages and Conversations

Swrve’s in-app messages and Conversations campaigns are available by default and require no additional integration steps. Use in-app messages or Conversations to send personalized messages to your app users while they’re using your app. For more information, see Intro to in-app messages and Intro to Conversations.

Conversations are currently not supported on OTT platforms.

To test in-app messages or Conversations in your app, you need to first create the campaign in Swrve. For more information, see Creating in-app messages and Creating Conversations.

In-app message and Conversation deeplinks

When creating in-app messages and Conversations in Swrve, you can configure message buttons to direct users to perform a custom action when clicked. For example, you might configure a button to direct the app user straight to the app store to provide a rating.

Swrve’s default behavior for in-app messages and conversations is to treat custom actions as URIs. If your app has an existing custom URL scheme defined in your AndroidManifest.xml, you can use this to deeplink in your app.

In-app message custom actions

For Conversations, deeplinks strings are always handled as URLs. For in-app messages, it is also possible to override this behavior of treating custom actions as URL deeplinks and integrate custom actions to direct users to a sale, website or other target when they click an in-app message. Use the following callback:

For example, to send Swrve events using custom actions, add a customButtonCallback like this:

Dismiss button callback

To be notified of a dismiss button callback action, add the following code:

Loading campaign resources and cache in the background

The Android SDK lets you decide if you want to wait on the previous in-app messaging campaign and resources when initializing. This allows you to obtain the previous values after calling init. In some instances, this can increase the time it takes for the Swrve SDK to initialize.

You can disable this behavior and load the cache in the background by setting the following property in your configuration object:

Sending events

The Swrve SDK automatically sends certain events and also enables you to track user behavior by sending custom events. (For a list of default Swrve events, see Segment and audience filters, Events.) In turn, you can use app-generated events to trigger in-app messages, while both app- and server-generated events help you define segments and perform in-depth analysis.

Custom events

To send a custom event, include the below example in a method where you want to send an event to Swrve.

Requirements for sending custom events:

  • Do not send the same named event with different case. For example, if you send tutorial.start, then ensure you never send Tutorial.Start.
  • Use a period (.) in your event names to organize their layout in the Swrve dashboard. Each ‘.’ creates a new branch in the Event name column of the Events report, and groups your events so they are easy to locate.
  • Do not send more than 1000 unique named events.
    • Do not add unique identifiers to event names. For example, Tutorial.Start.ServerID-ABDCEFG
    • Do not add timestamps to event names. For example, Tutorial.Start.1454458885
  • Do not use the swrve.* or Swrve.* namespace for your own events. This is reserved for Swrve use only. Custom event names beginning with Swrve. are restricted and cannot be sent.

Event payloads

You can add and send an event payload with every event. This allows for more detailed reporting around events and funnels.

Notes on associated payloads:

  • The associated payload should be a dictionary of key/value pairs; it is restricted to string and integer keys and values.
  • There is a maximum cardinality of 500 key-value pairs for this payload per event. This parameter is optional, but only the first 500 payloads are displayed in the dashboard. The data is still available in raw event logs and for audience filtering.
  • It is not currently possible to use payloads as triggers for push notifications. Use events for these purposes.
  • If you want to use event payloads to target your campaign audiences, you can configure up to 10 custom events with a maximum of 20 payloads per event for audience filtering purposes. For more information, see Targeting your audience by event payloads.

For example, if you want to track when a user starts the tutorial experience, it might make sense to send an event named tutorial.start and add a payload time that captures how much time it took the user to complete the tutorial.

Custom payloads on Swrve Conversation events

It is now possible to add custom payloads to a limited set of Swrve Conversation input events, to a maximum of five payloads. The custom payload is added to the following Swrve internal events (where the event name format is Swrve.Conversations.Conversation-ID.event_name):

  • star-rating – Triggered when a user selects a response in a star rating survey.
  • choice – Triggered when a user selects a choice in a text survey.
  • play – Triggered when a user selects “play”on a video.

Custom user properties

The Swrve SDK sends certain user properties by default and also enables you to assign custom properties to update the user’s status. (For a full list of the default user properties, see Assigning user properties.)

For example, you could create a custom user property called premium, and then target non-premium users and premium users in your campaigns.

When configuring custom properties for Android, the Swrve SDK only supports string values.

Example of group of user properties

Example of single date-typed user property

Use the Date object to send a DateTime user property; for example, the current date at the time of a user purchase:

Virtual economy events

To ensure virtual currency events are not ignored by the server, make sure the currency name configured in your app matches exactly the Currency Name you enter in the App Currencies section on the App Settings screen (including case-sensitive). If there is any difference, or if you haven’t added the currency in Swrve, the server will ignore the event and return an error event called Swrve.error.invalid_currency. Additionally, the ignored events are not included in your KPI reports. For more information, see Add your app.

If your app has a virtual economy, send the purchase event when users purchase in-app items with virtual currency.

Send the currency given event when you give users virtual currency. Examples include initial currency balances, retention bonuses and level-complete rewards.

In-app purchase events and validation

If your app has in-app purchases (IAPs), send the IAP event when a user purchases something with real money. This section details the IAP functions for unverified IAP events and for IAP events where the receipt can be verified. It also details how to enable IAP receipt validation for Google Play.

IAP functions

In the case of Android platforms (both native and Unity), Swrve does not automatically receive IAP receipts. For Swrve to verify IAPs, you must send an event for each purchase using the IAP API with the purchase receipt. For native Android, use Google Play services for this purpose. For Unity Android, you must use a Unity plugin to receive IAP receipts.

IAP functions for unverified IAP events (that is, for any app store other than Google Play):

IAP functions for IAP events where the receipt can be verified (for now, only Google Play is supported):


The following code is the initialization of the in-app purchase:

The following code is the callback function (executed when the purchase has been made):

It is also possible to create a SwrveIAPRewards object containing all the in-app currency and bundle items purchased (if the purchase contained other in-app items):

In this instance, iapPlay can be supplied with this object:

Enabling IAP receipt validation

To enable validation of Google Play IAP receipts, on the Integration Settings screen, in the IAP Validation section, in the Google Play Licensing Public Key field, you must enter and save the public key for licensing and in-app billing. This public key is used to verify digital signatures. Google signs data (the app itself for verifying that the app has been payed for or the in-app purchase receipt) and Swrve then uses the public key to verify every purchase before calculating your revenue KPIs. This ensures that your revenue figures are as accurate as possible (ignoring pirates and cheaters).

You usually enter the public key into Swrve when configuring the Integration Settings screen as part of the Swrve onboarding process. You can edit the settings on this screen at any time, as required. On the Settings menu, select Integration Settings.

To access your public key, access the Google Play Console and navigate to All applications > [Your app] > Development toolsServices and APIs. The public key is then displayed in the Your license key for this application section. For more information, see the Google Play In-App Billing documentation.

Verifying IAP receipt validation

Use the following events to monitor the success of IAP receipt validation:

  • swrve.valid_iap – fired if receipt verification has been successful and the receipt is valid.
  • swrve.invalid_iap – fired if receipt verification has been successful and the receipt is invalid.

Resource A/B testing

Integrating Swrve’s resource A/B testing functionality enables you to use Swrve to test how users respond to changes to the native app content. For more information about resource A/B testing, see Intro to resource A/B testing.

To get the latest version of a resource from Swrve using the Resource Manager, use the following:

If you want to be notified whenever resources change, you can add a callback function as follows:

Testing your integration

After you’ve completed the above, the next step is to test the integration. For more information, see Testing your integration.

Upgrade instructions

If you’re moving from an earlier version of the Android SDK to the current version, see the Android SDK upgrade guide for upgrade instructions.

SDK samples

For a general demo of the Swrve Android SDK, see the Swrve Android SDK samples on GitHub.