Android SDK Initialization

Overview

Initialization can begin by using the InitializationBuilder to create an Initializer object which will be used to initialize the SDK.

Create InitializationBuilder

In order to create the InitializationBuilder, three arguments are required:

NameDescription
contextA reference to the Application Context
environmentAn SBEnvironment, usually a member of the DefaultEnvironment enum type. This specifies what external Sensibill environment you wish your application to access. For example DefaultEnvironment.RECEIPTS_SANDBOX
tokenProviderA valid TokenProvider instance. It must be created by this point, but no calls to TokenProvider.provideTokenReplacement() will be made until SensibillSDK.start() is called.
The token provider needs to provide a unique token for each user. The token provided is used for communication with the Sensibill API. Each token is tied to a particular user, therefore it is important to ensure the token provided is always for the intended user.

Example:

val tokenProvider = object : TokenProvider {
    override fun provideTokenReplacement(
        oldAuthToken: String?,
        userIdentifier: String,
        listener: TokenProvider.OnTokenProviderListener
    ) {
        // Token replacement implementation
    }
}

val builder = InitializationBuilder(applicationContext, DefaultEnvironment.RECEIPTS_SANDBOX, tokenProvider)
final TokenProvider tokenProvider = new TokenProvider() {
    @Override
    public void provideTokenReplacement(@Nullable String oldAuthToken, @NonNull String userIdentifier, @NonNull TokenProvider.OnTokenProviderListener listener) {
        // Token replacement implementation
    }
}

InitializationBuilder builder = new InitializationBuilder(getApplicationContext(), DefaultEnvironment.RECEIPTS_SANDBOX, tokenProvider);

References:

The InitializationBuilder also has a number of optional configurations that can be applied, described below.

Optional: Event Listener

SDK events can be monitored by providing an Event Listener during initialization. A class which implements OnTrackEventListener can be passed into the InitializationBuilder. SDK events will trigger the .trackEvent() method in said class.

builder.onTrackEventListener(object : OnTrackEventListener {
        override fun trackEvent(
            event: AnalyticsEvent?,
            attributes: MutableMap<String, Any>?,
            superAttributes: MutableMap<String, Any>?
        ) {
            // Track Event
        }
        override fun onDestroy() {
            // Handle Tracker Destruction
        }
    })
InitializationBuilder builder = new InitializationBuilder(getApplicationContext(), DefaultEnvironment.BETA_SANDBOX)
        .onTrackEventListener(new OnTrackEventListener() {
            @Override
            public void trackEvent(AnalyticsEvent event, Map<String, Object> attributes, Map<String, Object> superAttributes) {
                // Track Event
            }
            @Override
            public void onDestroy() {
                // Handle Tracker Destruction
            }
        });

References:

Build and Initialize

Once the InitializationBuilder object is created and optional configurations are provided, it can be built by calling builder.build() and passed to the SensibillSDK.initialize() method along with an SDKInitializeListener.

NOTE: By the Sensibill SDK lifecycle rules, if SensibillSDK.initialize() and / or SensibillSDK.start() have already been called in this instance of the app, SensibillSDK.release() should be called before re-initializing. See below for example:

SensibillSDK.initialize(builder.build(), object : SDKInitializeListener {
    override fun onInitialized() {
        // Initialization succeeded, you can now continue with starting the SDK
    }
    override fun onInitializationFailed() {
        // Initialization failed. Handle initialization failure
    }
})
SensibillSDK.getInstance().initialize(builder.build(), new SDKInitializeListener() {
    @Override
    public void onInitialized() {
        // Initialization succeeded, you can now continue with starting the SDK
    }
    @Override
    public void onInitializationFailed() {
        // Initialization failed. Handle initialization failure
    }
});

References:

NOTE: Due to its asynchronous nature and potential time to complete, the initialize function has been changed to also require a listener. It is still possible to call SensibillSDK.initalize(Initializer), however this functionality is now deprecated and will soon be removed.