Skip to main content

🚀 Using the Demo​

We strongly recommend that you first run the framework-specific sample DEMO we have prepared for you. This will not only give you a firsthand experience of OpenIMSDK's features but will also assist you in quickly identifying and resolving issues during the actual integration process.

Integration Steps​

1. Add Dependencies​

implementation 'io.openim:android-sdk:latest@aar'
implementation 'io.openim:core-sdk:latest@aar'
implementation 'com.google.code.gson:gson:2.9.0'

2. Import Package​

import io.openim.android.sdk.OpenIMClient

3. Initialization​

InitConfig initConfig=new InitConfig(
Constant.getImApiUrl(),//SDK API address
Constant.getImWsUrl(),//SDK WebSocket address
getStorageDir(),//SDK database storage directory
);

OpenIMClient.getInstance().initSDK(
application, //Application
initConfig,//InitConfig
new OnConnListener() {
@Override
public void onConnectFailed(long code, String error) {
//Connection to server failed
}

@Override
public void onConnectSuccess() {
//Successfully connected to the server
}

@Override
public void onConnecting() {
//Connecting to the server...
}

@Override
public void onKickedOffline() {
//The current user was kicked offline
}

@Override
public void onUserTokenExpired() {
//The login token has expired
}
});

4. Set Listener​

// Set listener
// The SDK uses the 'set' method, setting multiple times will replace the previous setting. It's recommended to use middleware and the 'add' method for callback execution distribution, see the demo's IMEvent class for reference

// Callback when current user's profile changes
OpenIMClient.getInstance().userInfoManager.setOnUserListener(info -> {});
// Listener for receiving new messages, read receipts, and message recalls.
OpenIMClient.getInstance().messageManager.setAdvancedMsgListener(new OnAdvanceMsgListener() { }
// Listener for changes in friendships
OpenIMClient.getInstance().friendshipManager.setOnFriendshipListener(new OnFriendshipListener() {}
// Listener for new or changed conversations
OpenIMClient.getInstance().conversationManager.setOnConversationListener(new OnConversationListener() {}
// Listener for changes in group relationships
OpenIMClient.getInstance().groupManager.setOnGroupListener(new OnGroupListener() {}
// Signaling listener
OpenIMClient.getInstance().signalingManager.setSignalingListener(new OnSignalingListener() {}

5. Login​

Note: Set the listener first, then login

OpenIMClient.getInstance().login(new OnBase<String>() {
@Override
public void onError(int code, String error) {
}

@Override
public void onSuccess(String data) {
//Other API calls must ensure that the login callback is successful before proceeding

}
}, userID, imToken);