Leveraging OkVerify's foreground service

OkVerify's foreground service improves the reliability and stability of verification signals coming from your user's mobile phone. Here's how to use it

Due to the Background Execution Limits introduced in Android 8 as well as restrictions imposed by some device manufacturers its become increasingly difficult to determine accurate and timely verification signals within android applications. In order to ensure reliability of these verification signals, the library comes with an opt-in foreground service that you can leverage to decrease the amount of time it takes to verify an address.

Starting the foreground service

Configure a notification that'll be used to start the service

// Should be invoked one time on app start.
// (optional) OkHiNotification, use to start a foreground service to transmit verification signals to OkHi servers
int importance = Build.VERSION.SDK_INT >= Build.VERSION_CODES.N ? NotificationManager.IMPORTANCE_DEFAULT : 3;
OkVerify.init(getApplicationContext(), new OkHiNotification(
        "Verifying your address",
        "We're currently verifying your address. This won't take long",
        "OkHi",
        "OkHi Address Verification",
        "Alerts related to any address verification updates",
        importance,
        1, // notificationId
        2 // notification request code
));

After successfully starting verification on an address, the foreground service is started by default but you can start it manually if previously stopped.

// starts the foreground service
OkVerify.startForegroundService(getApplicationContext());

At this point a persistent notification will be shown to the user, signalling that the foreground service has indeed started successfully

Stopping the foreground service

Stopping the service is easy, simply make a call to the stopForegroundService static method that is available on the OkVerify class

// stops the running foreground service
OkVerify.stopForegroundService(getApplicationContext());

Stopping the foreground service does not stop verification of that address, the library will continue to use background services in order to obtain verification signals.

Determining whether the foreground service is running

You can make a call to the static method isForegroundServiceRunning method on the OkVerify class to determine whether a foreground service is present and running

OkVerify.isForegroundServiceRunning(getApplicationContext()); // returns true || false

Be sure to checkout our best practices doc for more information on foreground services

Customize notification icon & color

You can specify a custom default icon and a custom default color by adding these lines inside the application tag to set the custom default icon and custom color. You'll need to have already created the icon resource in your android application. Icon assets can be created by following these steps

<application>
    <meta-data android:name="io.okhi.core.branch_id" android:value="<my_branch_id>" />
    <meta-data android:name="io.okhi.core.client_key" android:value="<my_client_key>" />
    <meta-data android:name="io.okhi.core.environment" android:value="sandbox" /> <!-- set this to prod once ready to go live 🚀 -->
    <meta-data android:name="io.okhi.core.platform" android:value="android" />
    
    <!-- Set custom default icon for OkVerify's foreground service -->
    <meta-data android:name="io.okhi.android_background_geofencing.foreground_notification_icon" android:resource="@drawable/ic_person_pin" />
    <!-- Set custom default icon color for OkVerify's foreground service -->
    <meta-data android:name="io.okhi.android_background_geofencing.foreground_notification_color" android:resource="@color/colorAccent" />
    
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
    
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Be sure to checkout our best practices doc for more information on foreground services

Last updated