Chatroom

This section introduces how to integrate Hummer ChatRoom SDK Android version to the project.

1. Try Demo

YY provides a sample project for open-source and real-time voice calls on GitHub
Download Demo

2. Prerequisites

3. Development Environment Preparation

This section introduces how to create a project and integrate it with Hummer ChatRoom SDK.

3.1 Step 1: Create a Project

  1. Open Android Studio and click Start a new Android Studio project.
  2. Choose Choose your project > Phone and Tablet > Empty Activity, and then click Next.
  3. Enter the following content:
    • Name: project name, for example, HelloAgora
    • Package name: project package name, for example, io.agora.helloagora
    • Project location: project saving path
    • Language: programing language, for example, Java
    • Minimum API level: the lowest API level in the project

3.2 Step 2: Add Library Files

During integration, add a URL source to the Project build.gradle file:

buildscript {
    repositories {
        google()
        jcenter()
        maven {url "http://nexus.jocloud.com:8081/nexus/content/groups/public/" }
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        maven {url "http://nexus.jocloud.com:8081/nexus/content/groups/public/" }
    }
}

Add the following dependencies to the module build. gradle file:

implementation "com.hummer.im:core:2.6.109"
implementation "com.hummer.im:chatroom:2.6.109"

Obfuscate the code, then add the following code to proguard-rules.pro file:

-keep class com.hummer.im.**{*;}
-keepclassmembers class com.hummer.im.** {
   public *;
}

4. Basic Chatroom Operation

This section introduces how to operate chatroom functions. The following figure shows the chatroom API call sequence:

Logg in/out a Chatroom

sequenceDiagram App->>SDK: 1. initialize an SDK (init) App->>SDK: 2. log in an SDK (open) SDK-->>App: completion App->>SDK: 3. create a chatroom (createChatRoom) SDK-->>App: completion App->>SDK: 4. joining a chatroom (join) SDK-->>App: completion App->>SDK: 5. exit a chatroom (leave) SDK-->>App: completion App->>SDK: 6. log out an SDK(close) SDK-->>App: completion

Receive/Send Messages

sequenceDiagram UserA->>SDK: send a message (send) SDK-->>UserA: completion SDK->>UserB: receive a message (afterReceivingMessage) UserB->>SDK: send a message (send) SDK->>UserB: completion SDK->>UserA: receive a message (afterReceivingMessage)

4.1 Step 1: Initialize a Hummer SDK

Initialize the SDK to ensure that each component is completely initialized so the SDK can function normally. This is a synchronous API, meaning that only chatrooms of the same AppId can intercommunicate.

// content  Context of Android Activity
// appId  AppID issued by application developer
HMR.init(Demo.appContext(), currentAppId);

4.2 Step 2: Log in an SDK

Any use of the SDK is dependent on user login; therefore, upon re-login, the service will need to call the open method and report user information such as uid. This way the SDK can evaluate that user’s legality and establish corresponding user context. Once context is identified, it is also more convenient for identity authentication during SDK-server interactions.

// uid  uid of user currently logged in
// region  Region which chatroom belongs to, fill in “China” for the moment, and it will be globalized later
// tag  This parameter indicates the tag which user belongs to, which is used for message filtering later; for the moment, fill in blank set
// token  This parameter is a voucher used to verify the legitimacy of user
// Completion  Callback of SDK login; service may process the corresponding service logic differently according to the specific login result (success or failure)
HMR.open(uid, region, null, token, new HMR.Completion() {
    @Override
    public void onSuccess() {

    }

    @Override
    public void onFailed(Error err) {

    }
});

4.3 Step 3: Create a Chatroom

The chatroom must be created before it can be used, so you need to create a corresponding chatroom API.

// chatRoomInfo  Object of chatroom information, including name, description, announcement and extra information
// completion  Callback after chatroom creation: If creation succeeds, the corresponding Chatroom ID will be returned to the service for subsequent chatroom operation; if creation fails, callback will also include the reason.
HMR.getService(ChatRoomService.class).createChatRoom(chatRoomInfo, new HMR.CompletionArg<ChatRoom>() {
    @Override
    public void onSuccess(ChatRoom result) {

    }

    @Override
    public void onFailed(Error error) {

    }
});

4.4 Step 4: Join a chatroom

Currently the chatroom only supports single-end or multi-end access, so the specified access mode can search the background for corresponding configuration. Joining the chatroom via single-end access means that a second user joining the room will remove the first user from the room. Join the chatroom with join.

// chatRoom  ID of chatroom to be joined
// joinProps  Additional properties of chatroom to be joined; service may carry some additional properties when joining a chatroom
// completion  Callback after joining a chatroom. The service can perform different operations according to joining results. If joining fails, callback will include the reason for error.
HMR.getService(ChatRoomService.class).join(new ChatRoom(ChatRoom chatRoom,new HashMap<String, String>(), new Challenges.JoiningCompletion() {
    @Override
    public void onReceiveChallenge(Challenges.Password challenge) {
    }

    @Override
    public void onReceiveChallenge(Challenges.AppChallenge challenge) {
    }

    @Override
    public void onSucceed() {


    }

    @Override
    public void onFailure(@NonNull Error err) {

    }
});

4.5 Step 5: Send Messages

The message channel supports multiple message types, able to send public screen, unicast and broadcast signals. Implement this service by filling in Message with the corresponding message type. Send messages with the sendMessage method.

// message Message object to be sent
// compeltion  Callback of message sending. The service can perform different operations according to sending results.
HMR.getService(ChatService.class).send(new Message(new ChatRoom(
long channelId)),new Text(message)), new HMR.Completion() {
		@Override
		public void onSuccess() {

		}

		@Override
		public void onFailed(Error err) {

		}
});

4.6 Step 6: Receive Messages

Receive messages in the afterReceivingMessage callback method. After reception, the service can process messages based on message type and corresponding information.

// message  Object of message already received
@Override
public void afterReceivingMessage(@NonNull Message message) {

}

4.7 Step 7: Exit a Room

Once all the necessary logistics for joining the chatroom are processed, you can end interactions with the chatroom by exiting it. Listening to public screen or signaling messages and corresponding callback notifications will also be terminated.

// chatRoom  ID of chatroom to exit
// completion  Callback after exiting a chatroom. The service can perform different operations according to exiting results. If exiting fails, callback will include the reason for error.
HMR.getService(ChatRoomService.class).leave(new ChatRoom(long channelId), new HMR.Completion() {
    @Override
    public void onSuccess() {

    }

    @Override
    public void onFailed(Error err) {

    }
});

4.8 Step 8: Log out an SDK

The API ‘close’ is used to inform the SDK that the current user has logged out, ensuring that the SDK releases resources in time. If you need to use modules such as the chatroom again, you can log back in to the SDK through open. This is an asynchronous operation, so the last parameter is to inform of logout completion and the corresponding results. Pay attention—the corresponding close can only be called after open.

// Completion  Callback of SDK logout. The service can process the corresponding service logic according to the specific logout result (success or failure)
HMR.close(new HMR.Completion() {
    @Override
    public void onSuccess() {

    }

    @Override
    public void onFailed(Error err) {

    }
});

5. API Reference

These are all the APIs involved in this document.

6. Notices

Was this page helpful?

Helpful Not helpful
Submitted! Your feedback would help us improve the website.
Feedback
Top
<