Java

Receive new messages

You can display incoming messages by implementing the conversation listener.

Now, locate the getConversation method and add addMessageEventListener call:

private void getConversation() {
    client.getConversation(CONVERSATION_ID, new NexmoRequestListener<NexmoConversation>() {
        @Override
        public void onSuccess(@Nullable NexmoConversation conversation) {
            MainActivity.this.conversation = conversation;
            getConversationEvents(conversation);

            conversation.addMessageEventListener(new NexmoMessageEventListener() {
                @Override
                public void onTextEvent(@NonNull NexmoTextEvent textEvent) {
                    conversationEvents.add(textEvent);
                    updateConversationView();
                }

                @Override
                public void onAttachmentEvent(@NonNull NexmoAttachmentEvent attachmentEvent) {}

                @Override
                public void onEventDeleted(@NonNull NexmoDeletedEvent deletedEvent) {}

                @Override
                public void onSeenReceipt(@NonNull NexmoSeenEvent seenEvent) {}

                @Override
                public void onDeliveredReceipt(@NonNull NexmoDeliveredEvent deliveredEvent) {}

                @Override
                public void onTypingEvent(@NonNull NexmoTypingEvent typingEvent) {}
            });
        }

        @Override
        public void onError(@NonNull NexmoApiError apiError) {
            MainActivity.this.conversation = null;
            Toast.makeText(MainActivity.this, "Error: Unable to load conversation", Toast.LENGTH_SHORT);
        }
    });
}

Each time a new message is received public void onTextEvent(@NonNull NexmoTextEvent textEvent) listener is called, the new message will be added to the conversationEvents collection and updateConversationView method will be called to reflect the changes.

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: