Migration Guide
This guide takes an existing integration from Android SDK 1.x (up to 1.2.14) to 2.0.0. Version 2.0.0 is rewritten in Kotlin, removes Firebase from the SDK and aligns the API with the iOS SDK. Most 1.x calls still compile and are deprecated until 3.0, but a few changes need your action. Full list of changes: Changelog.
Go through the steps in order. Steps 1 to 6 are required, the rest are worth checking.
1. Repository and dependency
The SDK moved to a new Maven repository. Replace the old address in your settings.gradle.kts (or project level build.gradle.kts) and update the version:
repositories {
// remove: maven { url = uri("https://android-sdk.user.com") }
maven { url = uri("https://reposilite-mvn.user.com/releases") }
}
dependencies {
implementation("com.user:android-sdk:2.0.0")
}
Version 2.0.0 is published only in the new repository. The old address serves only 1.x versions.
Also good to know:
- Kotlin. 1.2.14 needed Kotlin 2.0 or later in your app. 2.0.0 works from Kotlin 1.8.
- Dependencies. The SDK now depends only on
kotlin-stdlib, OkHttp,androidx.core,androidx.browserandandroidx.annotation. It no longer brings Firebase, AppCompat, Material Components, RecyclerView, CardView, Retrofit, Gson, Glide, RxJava, Joda-Time or test libraries such as JUnit and Mockito. If your app uses any of them, declare them in your ownbuild.gradle.kts. - Permissions. The SDK adds
ACCESS_NETWORK_STATE, a normal permission that users are not asked for. - R8. The SDK no longer forces rules on your whole app, for example keeping line numbers and parameter names.
2. Firebase and the FCM token
The SDK no longer depends on Firebase and no longer fetches the token. Your app keeps Firebase Messaging, which it needs for push notifications, and passes the token to the SDK at launch and in onNewToken:
// Application.onCreate, after UserCom.initialize
FirebaseMessaging.getInstance().token.addOnSuccessListener { token ->
UserCom.default?.setFcmToken(token)
}
// FirebaseMessagingService
override fun onNewToken(token: String) {
UserCom.default?.setFcmToken(token)
}
If your app declared Firebase Messaging only through the SDK, add it to your app module: implementation("com.google.firebase:firebase-messaging") with the Firebase BoM, as in the Firebase guide. Details: Push Token.
3. Initialization
Replace the Builder with UserCom.initialize in Application.onCreate, and call ping():
| 1.x | 2.0.0 |
|---|---|
UserCom.Builder(this, sdkKey, integrationsKey, url).build() | UserCom.initialize(this, sdkKey, url) then UserCom.default?.ping() |
UserCom.getInstance() | UserCom.default (Kotlin), UserCom.getDefault() (Java), nullable |
.trackAllActivities(true) | trackScreen(name) where screens appear, see Screen Events |
.setDefaultCustomer(customer) | updateContact(contact, fcmToken) after initialize |
.openLinksInChromeCustomTabs(false) | UserCom.default?.customTabsIntent = null |
.setCustomTabsBuilder(builder) | UserCom.default?.customTabsIntent = builder.build() |
.setOnSdkInitializedListener(listener) | The callback of ping or updateContact |
The baseUrl must include https://. The Workspace Key (integrationsApiKey) is no longer used.
Builder.build() sent a ping. UserCom.initialize sends nothing: the contact is created by your first ping(), setFcmToken or updateContact. If a known user is logged in at launch, call updateContact with the user ID first. The deprecated Builder keeps working and still sends a ping.
Application.onCreateA push message can start your app's process without any activity. If the SDK is initialized later, for example in an activity, in-app messages received while the app was closed are lost. Initializing after the first activity resumed also delays in-app messages until the app comes back from the background. Move initialize to Application.onCreate.
UserCom.getInstance() no longer sends queued events, and it throws when the SDK is not initialized. Prefer UserCom.default?, which is null in that case.
4. Push notifications
| 1.x | 2.0.0 |
|---|---|
getInstance().onNotification(context, remoteMessage) | UserCom.default?.handleNotification(remoteMessage.data) |
getInstance().handleRouteFromNotification(intent) | UserCom.default?.handleNotificationClick(intent) in onCreate and onNewIntent |
UserComNotification.create(...) | isUserComNotification(data), and the link extra of the intent |
com.user.sdk.default_notification_icon / _color | com.google.firebase.messaging.default_notification_icon / _color |
handleRouteFromNotification counted a click every time it got the intent of a push notification, also after a rotation, when the activity was recreated or when the user opened the app from recents. handleNotificationClick returns true and counts a click only for a new tap. Call it in onCreate (when savedInstanceState is null) and in onNewIntent, and open the screen for the link extra only when it returns true. The value "None" means no link.
What to do:
- In
onMessageReceived, callhandleNotification(message.data). - In your launcher activity, call
handleNotificationClick(intent)inonCreateandonNewIntent. See Push Notifications.
The SDK ignores com.user.sdk.default_notification_icon and com.user.sdk.default_notification_color. They applied only to notifications shown in the foreground, so a notification looked different depending on the app state. Move their values to the Firebase meta-data keys, which Android uses in the background and the SDK uses in the foreground. See Icon and colour.
Also changed:
- The notification channel is renamed from "user.com" to "Marketing Notifications", with a description. Override
user_com_channel_nameanduser_com_channel_descriptionin your strings to use your own, in every language you need. Existing installations show the new name, or your own, after the update. See Channel name and description. - A notification shown by the SDK in the foreground opens your launcher activity the same way as a notification shown by Android. It no longer clears your app's activity stack.
- Notifications shown in the foreground now include the image from the panel.
5. Contacts and logout
| 1.x | 2.0.0 |
|---|---|
register(Customer().id("42").email(...).attr(...), callback) | updateContact(Contact(userId = "42", email = ..., attributes = ...), fcmToken, callback) |
RegisterResponse.getKey() | userKey |
logout() | logout(fcmToken, callback) |
register and Customer still work and are deprecated until 3.0. register now sends the data once and keeps no copy on the device. The contact data that 1.x stored on the device (from register or setDefaultCustomer) is deleted at the first launch of 2.0.0.
logout() in 1.x fetched the token from Firebase and removed it from the contact who was leaving. In 2.0.0 pass the token to logout(fcmToken). The SDK records a logout event, creates a new anonymous contact with the same token, and only then removes the token from the contact who left. The device keeps receiving push notifications on the anonymous contact.
Your contact stays the same after the update: the SDK takes over the contact key of 1.x at the first launch of 2.0.0.
The contact key is no longer part of the Android app backup. When a backup is restored on a new phone, the app starts with a new anonymous contact instead of the contact of the old phone. Identify it with updateContact at login.
6. Events
| 1.x | 2.0.0 |
|---|---|
sendEvent(UserComEvent) | sendEvent(name, attributes) |
trackScreen(activity), trackScreen(fragment) | trackScreen(name) |
sendEvent(eventName, parameters) | sendEvent(name, attributes, callback), same order |
sendProductEvent(productId, type, parameters) | sendProductEvent(productId, eventType, attributes, callback), same order |
UserComEvent and the trackScreen overloads for activities and fragments are removed. Calls with positional arguments to the remaining methods compile unchanged. Calls with Kotlin named arguments need the new parameter names.
Invalid input no longer throws. In 1.x, sendEvent with an empty name threw IllegalArgumentException. In 2.0.0 every failure is a UserComError in the optional callback and in UserCom.errorHandler, and the SDK never crashes your app because of the data you pass. Events sent before the first successful ping end with NotIdentified and are not queued.
@TrackArguments still compiles, but has no effect: screen events no longer carry the extras of the intent.
Events that 1.x had not sent yet are moved to the new queue at the first launch of 2.0.0 and sent once.
7. In-app messages
| 1.x | 2.0.0 |
|---|---|
setInAppNotificationClickHandler { url -> } | inAppMessageClickHandler = InAppMessageClickHandler { url -> true } |
setFontResolver { name -> } | fontResolver = InAppFontResolver { name, size -> } |
The deprecated methods still work. The old click handler takes every link, so the SDK never opens links while it is set. With inAppMessageClickHandler, return false to let the SDK open a link.
- Clicks are always reported, also when your handler takes the link and for buttons without a link. In 1.x, with a click handler set, clicks were not reported. Click numbers in the panel can rise.
- A message received in the background, on a locked phone or while the app was closed is shown at the next app open, within 6 hours. In 1.x it was dropped.
- "Displayed" is reported only once the user can see the message. Displayed numbers in the panel can change after the update.
- The message is a layer over your current screen instead of a separate activity. It moves with your navigation and survives a rotation. Your own dialogs appear above it.
- The back button closes the message and reports "dismissed". A tap outside the message no longer closes it.
New: UserCom.inAppMessagesEnabled pauses in-app messages, for example on a payment screen. See In-App Messages.
8. Removed features
| Removed | Replacement |
|---|---|
showPreloadContent, fetchPreloadContent | None. The feature no longer exists in Positive User. |
| The chat module | None |
UserComNotification, UserComEvent, UserComProductEvent | isUserComNotification, sendEvent(name, attributes), sendProductEvent(productId, eventType, attributes) |
9. Other behavior changes
Nothing to do in your app, but worth knowing:
- Delivery. Requests are kept in one queue that survives the app being closed. A failed request is sent up to 4 times in total, 30, 60 and 120 seconds apart. While the device is offline the queue waits for the network without using up retries. A 4xx answer, including 401 for a removed key, is final. A 429 is retried after the time the server asks for. See Delivery and Retries.
- Callbacks. Every callback runs on the main thread, and never before the call returns.
- Logs. The SDK writes nothing to Logcat by default. 1.x wrote the API key, the contact key, the email and the push token to Logcat. Turn on logs with
UserCom.debugLogging, see Logs. - Device data. The ping sends the Android version instead of the kernel version, the plain device model and the SDK version. It no longer sends whether the device is rooted.
- New.
handleEmailLinkopens your app from links in Positive User emails and counts the clicks.
After updating, run the steps in Test your integration. Test a push notification with the app in the background, with the app in the foreground, and after rotating the device, and check that each tap is counted once.