Kotlin

Receive new messages

You can display incoming messages by implementing the conversation listener.

Now, locate the getConversation method and add addMessageEventListener call:

private fun getConversation() {
    client.getConversation(CONVERSATION_ID, object : NexmoRequestListener<NexmoConversation?> {
        override fun onSuccess(conversation: NexmoConversation?) {
            this@MainActivity.conversation = conversation

            conversation?.let {
                getConversationEvents(it)
                it.addMessageEventListener(object : NexmoMessageEventListener {
                    override fun onTextEvent(textEvent: NexmoTextEvent) {
                        conversationEvents.add(textEvent)
                        updateConversationView()
                    }

                    override fun onAttachmentEvent(attachmentEvent: NexmoAttachmentEvent) {}
                    override fun onEventDeleted(deletedEvent: NexmoDeletedEvent) {}
                    override fun onSeenReceipt(seenEvent: NexmoSeenEvent) {}
                    override fun onDeliveredReceipt(deliveredEvent: NexmoDeliveredEvent) {}
                    override fun onTypingEvent(typingEvent: NexmoTypingEvent) {}
                })
            }
        }

        override fun onError(apiError: NexmoApiError) {
            conversation = null
            Toast.makeText(this@MainActivity, "Error: Unable to load conversation", Toast.LENGTH_SHORT)
        }
    })
}

Now each time a new message is received onTextEvent(textEvent: NexmoTextEvent) listener is called, the new message will be passed to updateConversation method and dispatched to the view via conversationEvents LiveData (same LiveData used to dispatch all the messages after loading conversation events).

Run the app

You can either launch the app on the physical phone (with USB Debugging enabled) or create a new Android Virtual Device. When device is present press Run button: