Suggestions

close search

Setting up authentication

In order to connect to Stringee Server, you will need access to the authentication credential: access_token. In production application, the access_token should be generated by your server described here: Client authentication, sample code generates access token here: https://github.com/stringeecom/server-samples/tree/master/access_token.

But to speed things up we will just hard code the value for now:

  1. In your Activity, declare a variable to store the access_token.
    private String token;
  2. Adjust the code by hard coding the value for the token:

To do this, go to Dashboard -> Tools -> Generate Access token and generates an access_token.

Connecting

Next, we will connect to Stringee Server. You must do this before you can send or receive a message

  1. Add a StringeeClient property to your Activity

    private StringeeClient client;

    The StringeeClient class is defined in Stringee SDK. It includes methods interacting with Stringee Server.

  2. Instantiate the StringeeClient object and call its connect(token) method:

    client = new StringeeClient(this);
    client.connect(token);
  3. To interact with Stringee Server connection, you will need register a StringeeConnectionListener interface with StringeeClient object before connecting.

    client.setConnectionListener(new StringeeConnectionListener() {
    @Override
    public void onConnectionConnected(StringeeClient stringeeClient, boolean isReconnecting) {
    }
    
    @Override
    public void onConnectionDisconnected(StringeeClient stringeeClient, boolean isReconnecting) {
    
    }
    
    @Override
    public void onIncomingCall(StringeeCall stringeeCall) {
    
    }
    
    @Override
    public void onConnectionError(StringeeClient stringeeClient, StringeeError stringeeError) {
    
    }
    
    @Override
    public void onRequestNewToken(StringeeClient stringeeClient) {
    
    }
    
    @Override
    public void onCustomMessage(String from, JSONObject msg) {
    
    }
    });        
    • When the client connects to Stringee Server, the onConnectionConnected(StringeeClient stringeeClient, boolean isReconnecting) method is called.
    • When the client disconnects to Stringee Server, the onConnectionDisconnected(StringeeClient stringeeClient, boolean isReconnecting) method is called
    • When the client fails to connect to Stringee Server, the onConnectionError(StringeeClient stringeeClient, StringeeError stringeeError) method is called.
    • When the token is expired, the onRequestNewToken(StringeeClient stringeeClient) is called. You will need re-generate a new token and connect again.
  4. To listen for object (Conversation, Message) change events, you will need register a ChangeEventListenter interface:

    stringeeClient.setChangeEventListenter(new ChangeEventListenter() {
    @Override
    public void onChangeEvent(StringeeChange change) {
    }
    });