Skip to main content

Installation

This guide adds the Positive User Android SDK to your app, connects it to your workspace and checks that contacts, events, push notifications and in-app messages reach the panel.

Naming

The artifact is com.user:android-sdk, the package is com.user.sdk, and the main class is UserCom.


Requirements​

  • Android 5.0 (API 21) or later
  • compileSdk 34 or later
  • Kotlin 1.8 or later, or Java 8 or later
  • A Mobile SDK key and your workspace domain, see Setup & Configuration
  • For push notifications and in-app messages: Firebase Cloud Messaging in your app, and a device or emulator with Google Play services for testing

The SDK depends on kotlin-stdlib 1.9, OkHttp 4, androidx.core, androidx.browser and androidx.annotation. It does not bundle Firebase: your app owns Firebase and passes the FCM token to the SDK. This model is called BYOT (bring your own token).


Step 1: Prepare the panel and Firebase​

Complete Setup & Configuration. You need:

  1. A Mobile SDK key (64 characters).
  2. Your workspace domain, for example your-domain.user.com.
  3. A Firebase service account key uploaded to the panel.

The Android SDK does not use the Workspace Key. The deprecated UserCom.Builder still takes it, and ignores it.

Check

You have the Mobile SDK key and the domain at hand, and the Firebase project in the panel is the same project as in your app's google-services.json.


Step 2: Add the SDK​

Add the Positive User Maven repository to your settings.gradle.kts:

dependencyResolutionManagement {
repositories {
google()
mavenCentral()
maven { url = uri("https://reposilite-mvn.user.com/releases") }
}
}

Then add the dependency to your app module's build.gradle.kts:

dependencies {
implementation("com.user:android-sdk:2.0.0")
}

Version 2.0.0 and later are published only in this repository. The old address https://android-sdk.user.com serves only 1.x versions, so remove it when you upgrade, see the Migration Guide.

The SDK adds three permissions to your merged manifest: INTERNET, ACCESS_NETWORK_STATE and POST_NOTIFICATIONS. It ships its own R8 rules, so release builds need no extra configuration.

Check

import com.user.sdk.UserCom compiles in your app module. If Gradle cannot find com.user:android-sdk:2.0.0, check that the repository is in the repositories block your project actually uses: settings.gradle.kts or the project level build.gradle.kts.


Step 3: Initialize the SDK​

Initialize the SDK once per process in Application.onCreate, then call ping() to create or update the contact. initialize does not send anything to the backend by itself.

import android.app.Application
import com.user.sdk.UserCom

class ExampleApp : Application() {

override fun onCreate() {
super.onCreate()
UserCom.initialize(this, "YOUR_MOBILE_SDK_KEY", "https://your-domain.user.com")
UserCom.default?.ping()
}
}

Declare the class in your AndroidManifest.xml, if it is not there yet:

<application
android:name=".ExampleApp"
...>
ParameterValue
applicationYour Application instance.
apiKeyYour 64 character Mobile SDK key. Not the REST API key.
baseUrlYour workspace address with the scheme, for example https://your-domain.user.com. Without https:// the SDK is not configured and every call ends with InvalidConfiguration.
fcmTokenOptional. The FCM token, if you already have it at launch.

initialize returns the SDK instance and sets UserCom.default. In Java, read it with UserCom.getDefault().

Initialize in Application.onCreate

A push message can start your app's process in the background, without any activity. Android then creates only your Application and your messaging service. If the SDK is initialized later, for example in an activity, UserCom.default is null at that moment: in-app messages received while the app was closed are lost. Initializing in an activity's onResume or later, as some cross platform wrappers do, also delays in-app messages until the app comes back from the background.

Check

Run the app. In the Positive User panel, a new anonymous contact appears in the contact list, with your device on its contact profile. UserCom.default?.userKey is no longer null.


Step 4: Turn on SDK logs while you integrate​

The SDK writes nothing to Logcat by default. Turn on its logs in debug builds to see what it sends and why it skips something:

UserCom.debugLogging = BuildConfig.DEBUG

Set it before initialize. Logs use the tag UserCom and never contain keys, tokens, request bodies or contact data. To send them to your own logger instead of Logcat, see Logs.


Step 5: Pass the FCM token​

Add Firebase Cloud Messaging to your app as described in the Firebase guide. Then give the token to the SDK at launch and whenever Firebase reports a new one:

// In Application.onCreate, after UserCom.initialize
FirebaseMessaging.getInstance().token.addOnSuccessListener { token ->
UserCom.default?.setFcmToken(token)
}

// In your FirebaseMessagingService
override fun onNewToken(token: String) {
UserCom.default?.setFcmToken(token)
}

setFcmToken stores the token and sends a ping, which registers the device for push notifications on the current contact. Login, logout and the other ways to pass the token are covered in Push Token.

Check

The contact appears when you filter Data → Contacts by FCM key with has any value. The panel does not show the token itself.


Step 6: Forward notifications to the SDK​

The SDK needs to see incoming messages to show in-app messages and push notifications in the foreground. It needs the intent of a tapped notification to count the click.

Received messages​

In your FirebaseMessagingService, pass every received message to handleNotification:

class PushService : FirebaseMessagingService() {

override fun onNewToken(token: String) {
UserCom.default?.setFcmToken(token)
}

override fun onMessageReceived(message: RemoteMessage) {
if (UserCom.default?.handleNotification(message.data) == true) return
// your own messages
}
}

Register the service in AndroidManifest.xml:

<service
android:name=".PushService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>

Taps​

A tap on a push notification opens your launcher activity. Pass its intent to handleNotificationClick in onCreate and in onNewIntent:

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (savedInstanceState == null) UserCom.default?.handleNotificationClick(intent)
}

override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
setIntent(intent)
UserCom.default?.handleNotificationClick(intent)
}

The SDK acts only on Positive User messages. handleNotification returns false for any other message, so your own notifications keep working. Opening a screen from the notification link: Push Notifications.

Check

Your service is the one registered for com.google.firebase.MESSAGING_EVENT in the merged manifest. If another library registers its own service for this action, only one of them receives messages.


Test your integration​

  1. Contact. Launch the app. A contact with your device appears in the panel.

  2. Event. Send a test event and check the result:

    UserCom.default?.sendEvent("sdk_test", mapOf("source" to "installation")) { error ->
    Log.d("SdkTest", "sdk_test sent: ${error ?: "OK"}")
    }

    The event appears on the contact's timeline.

  3. Push notification. On Android 13 or later, allow notifications for the app. Send a push notification to this contact from the panel, see how to launch a push notification campaign. Test once with the app in the background and once in the foreground. Tap it. The click is counted in the campaign statistics.

  4. In-app message. Send an in-app message to this contact while the app is open. It appears on screen and is counted as displayed. See in-app messages.


Common errors​

SymptomCauseFix
Gradle cannot resolve com.user:android-sdk:2.0.0The Positive User repository is missing, or added to a repositories block your project does not use.Add https://reposilite-mvn.user.com/releases as in Step 2.
The build fails because a dependency requires compileSdk 34Your compileSdk is lower than the AndroidX libraries of the SDK need.Raise compileSdk to 34 or later.
Every call ends with InvalidConfigurationThe base URL has no https://, or the API key is empty.Pass the full address, for example https://your-domain.user.com.
No contact appears in the panelNeither ping() nor setFcmToken is called, or the key or domain is wrong.Call ping() after initialization and read its result. Rejected with status 401 means a wrong or inactive Mobile SDK key.
sendEvent fails at once with NotIdentifiedThe event was sent before the first successful ping.Call ping() first. See Sending Events.
The contact cannot be selected in a push campaignNo FCM token was passed to the SDK.Repeat Step 5. See Troubleshooting.
Push notifications work, in-app messages do notonMessageReceived does not call handleNotification, or the SDK is initialized outside Application.onCreate.Repeat Step 3 and Step 6.

Next steps​


Need help?​

Contact Positive User support through the chat widget in your workspace or on user.com, or your Customer Success Manager. Include:

  • SDK version
  • Android version and device model
  • The contact key from UserCom.default?.userKey
  • The time of the test and what you expected to see in the panel
  • The error from the callback or UserCom.errorHandler, if any, and SDK logs with UserCom.debugLogging turned on