Live Chat SDK Kotlin
Configuration
Using Java 11 or Android API 26 and above.
To use the DolphinLiveChat SDK library, please follow these steps :
You need to import our library into yourr project
You can assamble dolphin lib and get arr file. (In android studio: Gradle-> Expand dolphinlib-> build -> double click assemble)
After the build is complete, you will get the aar location
Move the file to the “libs” folder of the client-app
You need to add some dependencies to your milk app .gradle file. Then, sync to compile 3Dolphins SDK. For example, include DolphinLiveChat into your project by placing it in the libs/ folder for example, as a Gradle compile dependency.
implementation(files("libs/dolphin-live-chat.aar"))
Create Instance
You must have the server url, client id, and client secret for the application before proceeding with authentication
This initialization only needs to be performed once during the application's lifecycle and can be applied at the initial startup. Here’s how to do it:
package com.imi.dolphin.livechat.DolphinLiveChatFactory;
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
EventBus.getDefault().register(this)
val dolphinLiveChat : DolphinLiveChat = DolphinLiveChatFactory.create()
val dolphinCore : DolphinCore = DolphinCore(
clientId = <CLIENT_ID>,
clientSecret = <CLIENT_SECRET>,
channelUrl = <CHANNEL_URL>
)
dolphinLiveChat.init(dolphinCore = dolphinCore)
}
}
Connect to Server
You need to initialize the Dolphin connector. Here's how to do it :
val dolphinProfile : DolphinProfile = DolphinProfile(
username = <USERNAME>,
email = <EMAIL>,
phoneNumber = <PHONE>,
uid = <UID>,
customerId = null
)
val dolphinLiveChatConnector : DolphinLiveChatConnector = dolphinLiveChat.getConnector(
dolphinProfile = dolphinProfile
)
Explanation Component:
DolphinProfile
Represents the identify of the user initiating the chat
username
Represents the name of the user
Represents the user's email
phoneNumber
Represents the user's phone number
uid
Used to determine whether this user session will create a new ticket or not
If you have additional fields to be sent to the sever, you can include the in customVariables. Below is an example of customeVariableas (JsonArray)
JSONArray customVariables = new JSONArray();
JSONObject field = new JSONObject();
field.put("name", "Doni");
customVariables.put(field);
dolphinProfile.customVariableJsonArray = customVariables
After successfully initializing the Dolphin connector, you can authenticate to the server using :
dolphinLiveChatConnector.connect(dolphinProfile)
Event Handler
The DolphiChat SDK uses EventBus to broadcast events throughout the application. You need register the object that will receive events from EventsBus. You can call it as follows :
EventBus.getDefault().register(this);
You can put this code in onResume(), onCreate() or onStart() method
Once you no longer need to capture the event, you should unregister the receiver by calling this method
EventBus.getDefault().unregister(this);
You can put this code in onDestroy() method
Receive Message
To receive message, do the following :
@Subscribe
public void onReceiveMessage(ChatEvent chatEvent) {
when (chatEvent.status) {
ChatEvent.Status.INCOMING -> {
// do something
// chatEvent.chat get chat data
// chatEvent.type get chat type
}
ChatEvent.Status.MESSAGE_READ -> {
// do something
// chatEvent.chat get chat data
// chatEvent.type get chat type
}
ChatEvent.Status.MESSAGE_SENT -> {
// do something
// chatEvent.chat get chat data
// chatEvent.type get chat type
}
ChatEvent.Status.MESSAGE_SEND_FAILED -> {
// do something
// chatEvent.status get chat status
}
ChatEvent.Status.AGENT_TYPING -> {
// do something
// chatEvent.chat get chat data
// chatEvent.type get chat type
}
else -> return
}
Send Message
DolphinConnect.getInstance().sendMessage("text message");
Send Media Message
DolphinConnect.getInstance().sendFile(file, DolphinChat.Type.IMAGE);
Binaries
Required dependencies for the 3Dolphins application
com.squareup.retrofit2:adapter-rxjava3:2.11.0
com.squareup.retrofit2:converter-gson:2.11.0
org.greenrobot:eventbus:3.3.1
com.fasterxml.jackson.module:jackson-module-kotlin:2.15.0
org.java-websocket:Java-WebSocket:1.6.0
com.squareup.okhttp3:logging-interceptor:4.12.0
com.squareup.okhttp3:okhttp:4.12.0
com.squareup.retrofit2:retrofit:2.11.0
io.reactivex.rxjava3:rxandroid:3.0.2
io.reactivex.rxjava3:rxjava:3.1.10
com.jakewharton.timber:timber:5.0.1
androidx.core:core-ktx:1.12.0
Last updated
Was this helpful?