This section shows you how to use the accelerator textchat.
There are two options for installing the OpenTok Accelerator TextChat library:
NOTE: The Accelerator TextChat includes the OpenTok Accelerator Core for Android.
- Clone the OpenTok Accelerator TextChat repo.
- From the OpenTok Text Chat Sample app project, right-click the app name and select New > Module > Import Gradle Project.
- Navigate to the directory in which you cloned OpenTok Text Chat Accelerator Pack, select accelerator-textchat-android, and click Finish.
- Open the build.gradle file for the app and ensure the following line has been added to the
dependencies
section:
compile project(':accelerator-textchat-android')
- Modify the build.gradle for your solution and add the following code snippet to the section labeled 'repositories’:
maven { url "http://tokbox.bintray.com/maven" }
- Modify the build.gradle for your activity and add the following code snippet to the section labeled 'dependencies’:
compile 'com.opentok.android:opentok-accelerator-textchat:1.0.+'
The TextChatFragment
class is the backbone of the text chat communication feature.
This class, which inherits from the android.support.v4.app.Fragment
class, sets up the text chat UI views and events, sets up session listeners, and defines a listener interface to monitor the changes.
public class TextChatFragment extends Fragment implements SignalListener {
. . .
}
The TextChatListener
interface monitors state changes in the TextChatFragment
, and defines the following methods:
public interface TextChatListener {
void onNewSentMessage(ChatMessage message);
void onNewReceivedMessage(ChatMessage message);
void onTextChatError(String error);
void onClose();
}
The following TextChatFragment
methods are used to initialize the app and provide basic information determining the behavior of the text chat functionality.
Feature | Methods |
---|---|
Set the maximum chat text length. | setMaxTextLength() |
Set the sender alias of the outgoing messages. | setSenderAlias() |
Set the listener object to monitor state changes. | setListener() |
For example, the following private method instantiates a TextChatFragment
object:
OTAcceleratorSession mSession = new OTAcceleratorSession (context, apikey, sessionId);
mSession.connect(token);
//...
private void initTextChatFragment() {
mTextChatFragment = TextChatFragment.newInstance(mSession, apikey);
getSupportFragmentManager().beginTransaction()
.add(R.id.textchat_fragment_container, mTextChatFragment).commit();
getSupportFragmentManager().executePendingTransactions();
}
This lines of code illustrates how to set the maximum message length to 1050 characters and set a new sender alias:
try {
mTextChatFragment.setSenderAlias("Tokboxer");
mTextChatFragment.setMaxTextLength(140);
mTextChatFragment.setListener(this);
} catch (Exception e) {
e.printStackTrace();
}
By implementing the TextChatFragment.TextChatListener
interface, the app can monitor both receiving and sending activity. For example, a message is successfully sent, or a message is sent with a code in the event of an error.
The method implementations shown below use the ChatMessage
object to send and receive messages.
The onNewSentMessage()
event is fired when a new individual ChatMessage
is sent to other client connected to the OpenTok session. To send a ChatMessage
, the TextChatFragment
uses the OpenTok signaling API.
The onNewReceivedMessage()
event is fired when a new ChatMessage
is received from the other client.
@Override
public void onNewSentMessage(ChatMessage message) {
Log.i(LOG_TAG, "New sent message");
}
@Override
public void onNewReceivedMessage(ChatMessage message) {
Log.i(LOG_TAG, "New received message");
}
Multiparty sample app using the Accelerator TextChat with best-practices for Android.
Interested in contributing? We ❤️ pull requests! See the Contribution guidelines.
We love to hear from you so if you have questions, comments or find a bug in the project, let us know! You can either:
- Open an issue on this repository
- See https://support.tokbox.com/ for support options
- Tweet at us! We're @VonageDev on Twitter
- Or join the Vonage Developer Community Slack
- Check out the Developer Documentation at https://tokbox.com/developer/