2. Initialization

Initialization sets up internal SDK components to allow the creation of a Sentiance user on the device, and perform detections in the background.

Only a limited set of SDK methods are allowed to be invoked before initializing the SDK.

1. Create an Application Class

Initialization must be done in the onCreate() method of your Application class. If you don't already have a custom application class, first create a new class that extends Application.

MyApplication.kt
import android.app.Application;

class MyApplication: Application() {
    override fun onCreate() {
        super.onCreate()
    }
}

Then reference this new class in the application tag of the AndroidManifest.xml

<application android:name="com.example.appname.MyApplication">
  <!-- Activities -->
</application>

2. Initialize the SDK

In the onCreate() method of your Application class, call initialize().

MyApplication.kt
override fun onCreate() {
    super.onCreate()

    val result = Sentiance.getInstance(this).initialize()
    if (result.isSuccessful) {
        Log.d(TAG, "Initialization succeeded");
    }
}

Initialization will normally succeed, unless an unexpected error or exception is encountered. In case of failure, you can check the reason via the returned result.

if (result.isSuccessful) {
    Log.d(TAG, "Initialization succeeded");
} else {
    Log.e(TAG, "Intialization failed with reason ${result.failureReason!!.name}", result.throwable);
}
Custom Notification

When the Sentiance SDK runs in the background, it initiates a foreground service and provides a notification to Android, which is displayed to the user while the service is active.

By customizing the notification you can tailor the appearance and behavior of the notification to match the branding and user experience of your application. This allows you to provide a seamless and consistent experience to your users while the Sentiance SDK operates in the background.

Please note that customizing the notification should be done carefully to ensure that it complies with Android guidelines and user preferences. By doing so, you can enhance the overall user experience and maintain a cohesive presentation of your app and the Sentiance SDK's background functionality.

You can specify the notification that the SDK should use by passing it to the SDK initializer as an option:

val options = SentianceOptions.Builder(context)
    .setNotification(nofitication, id)
    .build()
Sentiance.getInstance(context).initialize(options)

You can find a sample code snippet for creating a notification. Be sure to check our notification management page for more on the best practices of using service notifications.

Last updated