This section introduces how to integrate Hummer ChatRoom SDK Android version to the project.
YY provides a sample project for open-source and real-time voice calls on GitHub
Download Demo
This section introduces how to create a project and integrate it with Hummer ChatRoom SDK.
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 *;
}
This section introduces how to operate chatroom functions. The following figure shows the chatroom API call sequence:
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);
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) {
}
});
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) {
}
});
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) {
}
});
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) {
}
});
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) {
}
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) {
}
});
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) {
}
});
These are all the APIs involved in this document.