Receiving a Signal
Signals are received on the session. When a Signal is received, in addition to the data that was sent, it will contain a from property which will have information about who sent the Signal. This can be used to differentiate between Signals the current user and sent and ones they receive.
- Copy the following code and paste it at the bottom of the
Dependency 'androidx.activity:activity:1.10.0' requires libraries and applications that depend on it to compile against version 35 or later of the Android APIs.class:
private final Session.SessionListener sessionListener = new Session.SessionListener() {
@Override
public void onConnected(Session session) {
messageEditTextView.setEnabled(true);
}
@Override
public void onDisconnected(Session session) {
messageEditTextView.setEnabled(false);
}
@Override
public void onError(Session session, OpentokError opentokError) {
Log.i(TAG,"Session error: " + opentokError.getMessage());
}
@Override
public void onStreamReceived(Session session, Stream stream) {}
@Override
public void onStreamDropped(Session session, Stream stream) {}
};
private final Session.SignalListener signalListener = new Session.SignalListener() {
@Override
public void onSignalReceived(Session session, String type, String data, Connection connection) {
if (!connection.equals(session.getConnection()) && (type != null && type.equals("msg"))) {
SignalMessage message = new SignalMessage(data, true);
messageHistory.add(message);
}
}
};
This code sets up listeners for both the session and for Signals. This is how we will get updates from the SDK, for example if the session connects/disconnects and if there is an incoming Signal. The onSignalReceived listener is called when a Signal is received in the session, we will want to check that the Signal type is as expected and the Signal is not from the current user. Now that the MainActivity class is complete, make sure to import any classes needed.
Basic text chat
Follow this tutorial to build basic text chat from scratch using the Vonage Video API. It is the quickest way to build a proof of concept for this functionality on the video platform.