Sending Your First Message Using Sendbird In Android
Posted By : Prince Bhardwaj | 29-Nov-2017
In my previous blog, we have discussed how to integrate send bird in your android application. Now, question is how to use that in our application to chat or send message using that. So here is the solution for this -
Sending first message
1. Initializing SDK
Initialization helps you to bind SDK to android, allowing to respond to connection and state changes.Passed in app ID of sendbird application to you create for initialising SDK.
Sendbird.init() function should be called only once across entire application. onCreate() method of application class is suitable place for initialising
SendBird.init(APP_ID, Context context);
2. Connect
Connecting a user to sendbird server we need to use their User ID.Any new ID create a new user in sendbird application before connecting, and existing ID's will get you to "log in".
/*WRITE YOUR CODE HERE*/
SendBird.connect(USER_ID, new ConnectHandler() {
@Override
public void onConnected(User user, SendBirdException e) {
if (e != null) {
// Error.
return;
}
}
});
3.Create channel
In following example, an open channel will get created.Once the channel is created, users present in the sendbird application can communicate by entering in the channel.
Group channels these channels can also be created in same manner.If we need to add more users in the channel,the user already part of channel(creater) should invite them first.
/*WRITE YOUR CODE HERE*/
OpenChannel.createChannel(new OpenChannel.OpenChannelCreateHandler() {
@Override
public void onResult(OpenChannel openChannel, SendBirdException e) {
if (e != null) {
// Error.
return;
}
}
});
4. Enter channel
For viewing and sending the messages you need to enter in the channel.
/*WRITE YOUR CODE HERE*/
OpenChannel.getChannel(CHANNEL_URL, new OpenChannel.OpenChannelGetHandler() {
@Override
public void onResult(OpenChannel openChannel, SendBirdException e) {
if (e != null) {
// Error.
return;
}
openChannel.enter(new OpenChannel.OpenChannelEnterHandler() {
@Override
public void onResult(SendBirdException e) {
if (e != null) {
// Error.
return;
}
}
});
}
});
5. Send message
In the last, send message.Three types of messages are
- User message: It is plain text message.
- File message: It is binary file like image and pdf
- Admin message: It is special message send from dashboard and platform API.
/*WRITE YOUR CODE HERE*/
channel.sendUserMessage(MESSAGE, DATA, CUSTOM_TYPE, new BaseChannel.SendUserMessageHandler() {
@Override
public void onSent(UserMessage userMessage, SendBirdException e) {
if (e != null) {
// Error.
return;
}
}
});
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Prince Bhardwaj
Prince Bhardwaj is having Talent as Application Developer, he is an enthusiastic team player in Oodles Technologies.