Add live chat to an Android app
Add the Adrian SDK from JitPack, open the chat, show unread replies and identify customers.
The Android SDK supports Android 6 (API 23) and later, from Kotlin or Java. Source and releases: github.com/nugitech/adrianhq-sdk-android-kotlin.
1. Install
Add JitPack to your repositories in settings.gradle.kts:
dependencyResolutionManagement {
repositories {
google()
mavenCentral()
maven("https://jitpack.io")
}
}Then add the SDK to your app module:
dependencies {
implementation("com.github.nugitech:adrianhq-sdk-android-kotlin:0.1.0")
}The SDK brings its own internet permission, messenger screen and shrinker rules, so there's nothing to add to your manifest.
2. Configure: production
Call configure once, in your Application class:
class AcmeApplication : Application() {
override fun onCreate() {
super.onCreate()
Adrian.configure(this, workspaceId = "ws_your_workspace_id")
}
}3. Configure: local development
Point debug builds at the chat widget running on your machine:
if (BuildConfig.DEBUG) {
Adrian.configure(this, workspaceId = "ws_your_workspace_id", widgetUrl = "http://localhost:5181")
} else {
Adrian.configure(this, workspaceId = "ws_your_workspace_id")
}On an emulator or a USB-connected phone, forward the widget and API ports so localhost reaches your computer:
adb reverse tcp:5181 tcp:5181
adb reverse tcp:8000 tcp:8000Android blocks plain HTTP by default. Allow it for localhost in debug builds only, with src/debug/res/xml/network_security_config.xml:
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="false">localhost</domain>
</domain-config>
</network-security-config>and android:networkSecurityConfig="@xml/network_security_config" on the <application> in src/debug/AndroidManifest.xml.
4. Open the chat and show unread replies
Drop the launcher into a layout. It opens the chat on tap and shows the unread badge:
<com.adrianhq.chat.AdrianLauncherButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="20dp" />Or open it yourself and listen for changes:
class MainActivity : AppCompatActivity() {
private val listener = object : AdrianListener {
override fun onUnreadCountChanged(count: Int) {
helpBadge.text = "$count new"
helpBadge.isVisible = count > 0
}
}
override fun onStart() {
super.onStart()
Adrian.addListener(listener)
Adrian.trackScreen("Home")
}
override fun onStop() {
Adrian.removeListener(listener)
super.onStop()
}
}
// From a help button
Adrian.present(context)5. Identify customers and sign out
// After sign-in
Adrian.identify(name = user.name, email = user.email)
// On sign-out: the next person on this device starts a new conversation
Adrian.reset()identify labels the conversation with a name and email. It doesn't prove who the customer is, so verify identity before account changes, as you would on your website.
6. Events and Java
AdrianListener has onReady, onOpened, onClosed, onUnreadCountChanged, onMessageReceived, onPrimaryColorChanged and onUnavailable. The same state is available as Adrian.unreadCount, primaryColor, latestMessage, isAvailable and isPresented.
From Java, the methods are static, for example Adrian.present(context). Read the properties through Adrian.INSTANCE, for example Adrian.INSTANCE.getUnreadCount().
Was this article helpful?