Skip to content

Messaging

Ray edited this page Jul 31, 2021 · 74 revisions

Overview

StraaS Messaging SDK provides a easy way to establish a Real-Time chatroom or message board!

straas-messaging only provides non-ui part of a chatroom, you can use straas-messaging-ui instead if you want to create a chatroom fast with default UI.

Getting Started

This shows you how to establish a chatroom step by step

Include

Add dependency on jCenter using Gradle.

X.X.X is your preferred version. For the version information, see CHANGELOG

compile 'io.straas.android.sdk:straas-messaging:X.X.X'

Validation

Fill up Using Straas SDK to enable messaging SDK.

Connect to a chatroom

  • Create ChatRoomManager and do initialization. This method will check if developer has passed StraaS SDK-Credential. Then you can get the initialization result from the returned Task.
ChatroomManager.initialize().addOnSuccessListener(new OnSuccessListener<ChatroomManager>() {
    @Override
    public void onSuccess(ChatroomManager chatroomManager) {
       //Keep your ChatroomManager
    }
}).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception e) {
       //initialization fails
    }
});
  • To get in a chatroom, you can invoke connect(...). You could decide the target chatroom name, who connect to this chatroom, and whether it is a personal chatroom or not.
chatroomManager.connect(CHATROOM_NAME, MemberIdentity.ME, false)
    .addOnSuccessListener(new OnSuccessListener<Void>() {
        @Override
        public void onSuccess(Void aVoid) {
            //Connection success
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            //Connection failure
        }
    });

Get users/messages

  • After getting in a chatroom, you can use getUsers(...), getMessages(...) or other methods to get the information required for setting up your UI.
MessageRequest request = new MessageRequest.Builder()
    .latestDate(System.currentTimeMillis())
    .page(1)
    .perPage(20)
    .build();
chatroomManager.getMessages(request).addOnCompleteListener(new OnCompleteListener<Message[]>() {
    @Override
    public void onComplete(@NonNull Task<Message[]> task) {
        if (task.isSuccessful()) {
            Message[] msgs = task.getResult();
        } else {
            Exception error = task.getException();
        }
    }
});

Block/revive users

chatroomManager.blockUsers(users).addOnCompleteListener(new OnCompleteListener<Void>() {
    @Override
    public void onComplete(@NonNull Task<Void> task) {
        if (task.isSuccessful()) {
            //success
        } else {
            //failure
        }
    }
});

Remove message

  • After getting in a chatroom, you can use removeMessage(...) to remove a message.
chatroomManager.removeMessage(message).addOnCompleteListener(new OnCompleteListener<Void>() {
    @Override
    public void onComplete(@NonNull Task<Void> task) {
        if (task.isSuccessful()) {
            //success
        } else {
            //failure         
        }
    }
});

Listen events

Create a EventListener and use it in addEventListener(...) to listen events. For example, when onMessageAdded(...) callback is invoked, you would get the real-time messages.

Disconnect

  • If you want to leave the chatroom, remember to call disconnect(...) method to notify server your leaving. You could get the result in Listen events.
chatroomManager.disconnect();

You could see API document for more details.