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

int importance = Build.VERSION.SDK_INT >= Build.VERSION_CODES.N ? NotificationManager.IMPORTANCE_DEFAULT : 3;
// Should be invoked one time on app start.
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,
    R.mipmap.ic_launcher,
    1, // notificationId
    2 // notification request code
));

After successfully starting verification on an address, call the static startForegroundService method available in the OkVerify class.

okVerify.start(user, workAddress, new OkVerifyCallback<String>() {
    @Override
    public void onSuccess(String result) {
        showMessage("Successfully started verification for: " + result);
        // start the foreground service
        OkVerify.startForegroundService(getApplicationContext());
    }

    @Override
    public void onError(OkHiException e) {
        showMessage("Something went wrong: " + e.getCode());
    }
});

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

Last updated