OkVerify Android

The OkVerify Android library enables you to verify a user's OkHi address automatically

Getting started

Make sure you have completed the OkHi on your android app steps

Installation

In your app module build.gradle dependency, add the following dependency and sync gradle files

dependencies {
  implementation 'com.github.okhi:android-okverify:v1.4.2'
}

Check for the latest release on github.

Usage

Create an instance of the OkVerify class to be used later in your class.

Make sure to invoke the static init method once on app start.

public class MainActivity extends AppCompatActivity {

    private OkVerify okVerify;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Initialise the okverify library. Must be done onCreate
        try {
            okVerify = new OkVerify.Builder(this).build();
        } catch (OkHiException exception) {
            exception.printStackTrace();
        }

        // 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
        ));
    }
}

Start / Stop OkHi address verification

public class MainActivity extends AppCompatActivity {

    private OkVerify okVerify;
    private OkHiLocation workAddress = new OkHiLocation("<okhi_location_id>", -1.266429, 36.765606);
    private OkHiUser user = new OkHiUser.Builder("+254711234567")
                    .withFirstName("Juliet")
                    .withLastName("Zuri")
                    .build();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Initialise the okverify library. Must be done onCreate
        try {
            okVerify = new OkVerify.Builder(this).build();
        } catch (OkHiException exception) {
            exception.printStackTrace();
        }

        // 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
        ));
    }
    
    private void startAddressVerification() {                    
      okVerify.start(user, workAddress, new OkVerifyCallback<String>() {
          @Override
          public void onSuccess(String result) {
            showMessage("Successfully started verification for: " + result);
          }
          @Override
          public void onError(OkHiException e) {
            showMessage("Something went wrong: " + e.getCode());
          }
      });
    }
    
    private void stopAddressVerification() {
      OkVerify.stop(getApplicationContext(), work.getId());
    }
    
    private void startForegroundVerification() {
        try {
            // start a foreground service that'll improve the stability and reliability of verification signals
            OkVerify.startForegroundService(getApplicationContext());
        } catch (OkHiException e) {
            e.printStackTrace();
        }
    }
    
    private void stopForegroundVerification() {
        // stops the running foreground service
        OkVerify.stopForegroundService(getApplicationContext());
    }

    private boolean checkForegroundService() {
        // checks if the foreground service is running
        return OkVerify.isForegroundServiceRunning(getApplicationContext());
    }
}

Working with OkCollect

public class MainActivity extends AppCompatActivity {

    private OkCollect okCollect;
    private OkVerify okVerify;
    private OkHi okhi;

    // define a theme that'll be applied to OkCollect
    private final OkHiTheme theme = new OkHiTheme.Builder("#ba0c2f").setAppBarLogo("https://cdn.okhi.co/icon.png").setAppBarColor("#ba0c2f").build();

    // configure any optional features you'd like
    private final OkHiConfig config = new OkHiConfig.Builder().withStreetView().build();

    // define a user
    private final OkHiUser user = new OkHiUser.Builder("+254712345678").withFirstName("Julius").withLastName("Kiano").build();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // initiate okcollect using the values defined above
        try {
            okhi = new OkHi(this);
            okCollect = new OkCollect.Builder(this).withTheme(theme).withConfig(config).build();
            okVerify = new OkVerify.Builder(this).build();
            // 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
            ));
        } catch (OkHiException exception) {
            exception.printStackTrace();
        }
    }

    public void onCreateAddressPress (View v) {
        launchOkCollect();
    }

    private void launchOkCollect() {
        boolean canStartOkCollect = canStartAddressVerification();
        if (canStartOkCollect) {
            // launch okcollect
            okCollect.launch(user, new OkCollectCallback<OkHiUser, OkHiLocation>() {
                @Override
                public void onSuccess(OkHiUser user, OkHiLocation location) {
                    showMessage("Address created "+user.getPhone()+" "+location.getId());
                    startAddressVerification(user, location);
                }
                @Override
                public void onError(OkHiException e) {
                    showMessage("Error "+e.getMessage());
                }
            });
        }
    }

    private void startAddressVerification(OkHiUser user, OkHiLocation location) {
        okVerify.start(user, location, new OkVerifyCallback<String>() {
            @Override
            public void onSuccess(String result) {
                showMessage("Successfully started verification for: " + result);
            }
            @Override
            public void onError(OkHiException e) {
                showMessage("Something went wrong: " + e.getCode());
            }
        });
    }

    //handler class that extends OkHiRequestHandler
    class Handler implements OkHiRequestHandler<Boolean> {
        @Override
        public void onResult(Boolean result) {
            if (result) launchOkCollect();
        }
        @Override
        public void onError(OkHiException exception) {
            showMessage(exception.getMessage());
        }
    }

    // Define a method you'll use to check if conditions are met to start address creation
    private boolean canStartAddressVerification() {
        Handler requestHandler = new Handler();
        // Check and request user to enable location services
        if (!OkHi.isLocationServicesEnabled(getApplicationContext())) {
            okhi.requestEnableLocationServices(requestHandler);
        } else if (!OkHi.isGooglePlayServicesAvailable(getApplicationContext())) {
            // Check and request user to enable google play services
            okhi.requestEnableGooglePlayServices(requestHandler);
        } else if (!OkHi.isBackgroundLocationPermissionGranted(getApplicationContext())) {
            // Check and request user to grant location permission
            okhi.requestBackgroundLocationPermission("Hey we need location permissions", "Pretty please..", requestHandler);
        } else {
            return true;
        }
        return false;
    }

    private void showMessage (String message) {
        Log.v("MainActivity", message);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        // Pass permission results to okcollect
        okhi.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // Pass activity results results to okcollect
        okhi.onActivityResult(requestCode, resultCode, data);
    }
}

Check out our full OkVerify integration example

FAQs

Building with pro-guard enabled

If you have minifyEnabled set to true in your build.gradle file located android/app/build.gradle, you'll need to modify your proguard-rules.pro file, located android/app/proguard-rules.proas shown bellow to include classes required by the library to run.

-dontwarn sun.reflect.**
-dontwarn java.beans.**
-dontwarn sun.nio.ch.**
-dontwarn sun.misc.**

-keep class com.esotericsoftware.** {*;}

-keep class java.beans.** { *; }
-keep class sun.reflect.** { *; }
-keep class sun.nio.ch.** { *; }

-keep class com.snappydb.** { *; }
-dontwarn com.snappydb.**

# If you don't use OkHttp as a dep and the following

-dontwarn org.codehaus.mojo.animal_sniffer.*
-dontwarn javax.annotation.**
-keepnames class okhttp3.internal.publicsuffix.PublicSuffixDatabase
-dontwarn org.codehaus.mojo.animal_sniffer.*
-dontwarn okhttp3.internal.platform.ConscryptPlatform
-dontwarn org.conscrypt.ConscryptHostnameVerifier

Last updated