Initialize the Oracle Android Client SDK in Your App

Here's what you need to know about initializing the Android SDK in your app.

Initialize Oracle Android Client SDK in your Application class. Initialize the SDK describes the different methods that you can use to initialize the SDK. The JavaDoc that's included with the SDK describes all of the available classes.

If you're connecting to a channel with client authentication disabled, pass false as the second parameter to the BotsConfiguration.BotsConfigurationBuilder() constructor function.
import android.app.Application;
import oracle.cloud.bots.mobile.core.Bots;
import oracle.cloud.bots.mobile.core.BotsCallback;
import oracle.cloud.bots.mobile.core.BotsConfiguration;
import oracle.cloud.bots.mobile.core.BotsSDKException;

public class YourApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        try {
                BotsConfiguration botsConfiguration = new BotsConfiguration.BotsConfigurationBuilder(<SERVER_URI>, false, getApplicationContext()) // Configuration to initialize the SDK
                    .channelId(<CHANNEL_ID>)
                    .userId(<USER_ID>)
                    .build();

                Bots.init(this, botsConfiguration, new BotsCallback() {  // Initialize the SDK
                    @Override
                    public void onSuccess(Response paramResponse) {
                        // Handle init success
                    }
                    @Override
                    public void onFailure(Response paramResponse) {
                        // Handle init failure
                    }
                });
        } catch (BotsSDKException e) {
           // Handle Exceptions thrown by SDK
        }
    }
}

If you are connecting to a channel with client authentication enabled, you need to make some minor modifications: along with passing true as the second parameter to the BotsConfiguration.BotsConfigurationBuilder() constructor function, you also need to set the authTokenProvider property with the instance of type AuthenticationTokenProvider that can be used to generate and pass the JWT token.

The class should implement the AuthenticationTokenProvider interface, which then overrides the getAuthToken() function to generate and return a JWT token. The function will be used by the SDK to generate a new token whenever it needs to establish a new connection and existing token is expired. The code would look something like this:
import android.app.Application;
import oracle.cloud.bots.mobile.core.AuthenticationTokenProvider;
import oracle.cloud.bots.mobile.core.Bots;
import oracle.cloud.bots.mobile.core.BotsCallback;
import oracle.cloud.bots.mobile.core.BotsConfiguration;
import oracle.cloud.bots.mobile.core.BotsSDKException;

public class YourApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        try {
                BotsConfiguration botsConfiguration = new BotsConfiguration.BotsConfigurationBuilder(<SERVER_URI>, true, getApplicationContext()) //  Configuration to initialize the SDK
                    .authTokenProvider(new AuthTokenProvider())
                    .build();

                Bots.init(this, botsConfiguration, new BotsCallback() {  // Initialize the SDK
                    @Override
                    public void onSuccess(Response paramResponse) {
                        // Handle init success
                    }
                    @Override
                    public void onFailure(Response paramResponse) {
                        // Handle init failure
                    }
                });
        } catch (BotsSDKException e) {
           // Handle Exceptions thrown by SDK
        }
    }

    private class AuthTokenProvider implements AuthenticationTokenProvider {
        @Override
        public String getAuthToken() {
            // Generate a new JWT Token and return
        }
    }
}
Display the user interface:
import oracle.cloud.bots.mobile.ui.ConversationActivity;
 
...
 
 
ConversationActivity.show(this);