The Chat SDK simplifies chat features into an effortless and straightforward process. To send your first message, do the following steps:
Note: The methods in the following steps are all asynchronous, excluding the
SendBird.init()
method. This means that when using asynchronous methods, your client app must receive success callbacks from Sendbird server through their callback handlers in order to proceed to the next step. A good way to do this is the nesting of methods: Go to Step 4: Enter the channel to learn more about how you can nest theopenChannel.enter()
in theOpenChannel.getChannel()
method.
Initialization binds the Chat SDK to Android’s context, thereby allowing it to respond to connection and state changes. To the init()
method, pass the App ID of your Sendbird application in the dashboard to initialize the Chat SDK.
Note: The
SendBird.init()
method must be called once across your Android client app. It is recommended to initialize the Chat SDK in theonCreate()
method of theApplication
instance.
// Initialize SendBird instance to use APIs in your app.
SendBird.init(APP_ID, getApplicationContext());
Connect a user to Sendbird server using their unique user ID. Any untaken user ID is automatically registered as a new user to your Sendbird application before being connected, while an existing ID is allowed to log in directly.
Note: Go to the Authentication page to learn more about authenticating with an access token.
// The USER_ID below should be unique to your Sendbird application.
SendBird.connect(USER_ID, new SendBird.ConnectHandler() {
@Override
public void onConnected(User user, SendBirdException e) {
if (e != null) {
// Handle error.
}
// The user is connected to Sendbird server.
}
});
Create an open channel. Once created, all users in your Sendbird application can easily participate in the channel.
Note: In a similar fashion, you can create a group channel by inviting users as new members to the channel.
OpenChannel.createChannel(new OpenChannel.OpenChannelCreateHandler() {
@Override
public void onResult(OpenChannel openChannel, SendBirdException e) {
if (e != null) {
// Handle error.
}
// An open channel is successfully created.
// Through the "openChannel" parameter of the onResult() callback method,
// you can get the open channel's data from the result object that Sendbird server has passed to the onResult().
...
}
});
Enter the channel to send and receive messages.
// The CHANNEL_URL below can be retrieved using the openChannel.getUrl().
OpenChannel.getChannel(CHANNEL_URL, new OpenChannel.OpenChannelGetHandler() {
@Override
public void onResult(OpenChannel openChannel, SendBirdException e) {
if (e != null) {
// Handle error.
}
// Call the instance method of the result object in the "openChannel" parameter of the onResult() callback method.
openChannel.enter(new OpenChannel.OpenChannelEnterHandler() {
@Override
public void onResult(SendBirdException e) {
if (e != null) {
// Handle error.
}
// The current user successfully enters the open channel,
// and can chat with other users in the channel by using APIs.
...
}
});
}
});
Finally, send a message to the channel. There are three types: a user message is a plain text, a file message is a binary file, such as an image or PDF, and an admin message is a plain text sent through the dashboard or Chat API.
openChannel.sendUserMessage(MESSAGE, new BaseChannel.SendUserMessageHandler() {
@Override
public void onSent(UserMessage userMessage, SendBirdException e) {
if (e != null) {
// Handle error.
}
// The message is successfully sent to the channel.
// The current user can receive messages from other users through the onMessageReceived() method of an event handler.
...
}
});