Leveraging OkVerify's foreground service

OkVerify's foreground service improves the reliability and stability of verification signals coming from your user's Android device. 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. If you followed the previous guide, this should already be setup in your index.js file

OkVerify.init({
  channelId: 'okhi',
  channelDescription: 'OkHi verification alerts',
  channelName: 'OkHi Verification',
  title: 'Verification in progress',
  text:
    "We're currently verifying your address, you can ignore this notification",
  notificationId: 1,
  notificationRequestCode: 2
});

The foreground service is started by default once verification of an address starts, but can also be started again if previously stopped using the startForegroundService function

import { startForegroundService } from '@okhi/react-native-okverify';
 
const startedForegroundService = await startForegroundService(); // returns true || false indicating success of the process 

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 function

import { stopForegroundService } from '@okhi/react-native-okverify'
// stops the running foreground service
const stoppedForeground = await stopForegroundService();

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 isForegroundServiceRunning function to determine whether a foreground service is present and running

import { isForegroundServiceRunning } from '@okhi/react-native-okverify'
// stops the running foreground service
const serviceRunning = await isForegroundServiceRunning();

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="react-native" />
    
    <!-- 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