This repository has been archived by the owner on Jan 27, 2023. It is now read-only.
/
SendCustomEventActivityKotlin.kt
50 lines (42 loc) · 1.88 KB
/
SendCustomEventActivityKotlin.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.vonage.sample.channel.messaging
import android.os.Bundle
import android.os.PersistableBundle
import androidx.appcompat.app.AppCompatActivity
import com.nexmo.client.NexmoClient
import com.nexmo.client.NexmoConversation
import com.nexmo.client.request_listener.NexmoApiError
import com.nexmo.client.request_listener.NexmoRequestListener
import timber.log.Timber
class SendCustomEventActivityKotlin : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
super.onCreate(savedInstanceState, persistentState)
// No need for client initialization here. Client initialization is already done in BaseApplication class.
// NexmoClient.Builder().build(this)
val client = NexmoClient.get()
client.login("JWT token")
getConversation(client)
}
private fun getConversation(client: NexmoClient) {
client.getConversation("CONVERSATION_ID", object : NexmoRequestListener<NexmoConversation> {
override fun onSuccess(conversation: NexmoConversation?) {
Timber.d("Conversation loaded")
conversation?.let {
sendCustomEvent(it, "my_custom_event", hashMapOf("key" to "data"))
}
}
override fun onError(apiError: NexmoApiError) {
Timber.d("Error: Unable to load conversation ${apiError.message}")
}
})
}
private fun sendCustomEvent(conversation: NexmoConversation, eventType: String, data: HashMap<String, Any>) {
conversation.sendCustomEvent(eventType, data, object : NexmoRequestListener<Void> {
override fun onSuccess(p0: Void?) {
Timber.d("Custom event sent")
}
override fun onError(apiError: NexmoApiError) {
Timber.d("Custom event error")
}
})
}
}