Skip to content

Integrate the Tapkey Mobile SDK

Warning

This documentation refers to version 1.15.5.0 and later. This documentation may not be appropriate for older versions. Take a look at the upgrade section for upgrading to the latest version of the Tapkey Mobile SDK.

Sample App

The Tapkey Mobile SDK sample app is available on GitHub, demonstrating how to use the Tapkey Mobile SDK in order to offer the relevant functionality.

Add Tapkey Mobile SDK as a Gradle Dependency

The Tapkey Mobile SDK is published in a private Maven repository. To tell Gradle where to find the artefacts, add https://maven.tapkey.com as repository and add the latest version of com.tapkey.android:Tapkey.MobileLib as dependency to the project's build.gradle file.

repositories {
    maven { url "https://maven.tapkey.com" }
}

dependencies {
    implementation 'com.tapkey.android:Tapkey.MobileLib:1.15.5.0'
}

android {
    ...
}

...

Bootstrap the Tapkey Mobile SDK

The Tapkey Mobile SDK expects the Application class of the target App to implement the TapkeyAppContext interface, which returns a singleton instance of the AndroidTapkeyServiceFactory. The singleton should be created in the Application.onCreate() method using the TapkeyServiceFactoryBuilder.

  1. Implement the TapkeyAppContext interface
  2. Use the TapkeyServiceFactoryBuilder to create an instance of AndroidTapkeyServiceFactory in the Application.onCreate().
  3. Set the default CookieHandler to allow persistence of authentication cookies.
  4. Implement getTapkeyServiceFactory() which returns the AndroidTapkeyServiceFactory singleton.
public class App extends Application implements TapkeyAppContext{

    private AndroidTapkeyServiceFactory tapkeyServiceFactory;

    @Override
    public void onCreate() {
        super.onCreate();

        /*
         * Build the TapkeyServiceFactory instance.
         */
        this.tapkeyServiceFactory = new TapkeyServiceFactoryBuilder()
            .build(this);

        /*
         * The Tapkey Mobile SDK requires cookies to be persisted in order to survive
         * app/device restarts because server authentication involves authentication
         * cookies. Persistence is done using the cookie store provided by the
         * TapkeyServiceFactory instance.
         */
        CookieHandler.setDefault(new CookieManager(tapkeyServiceFactory.getCookieStore(),
            CookiePolicy.ACCEPT_ORIGINAL_SERVER));
     }

    @Override
    public AndroidTapkeyServiceFactory getTapkeyServiceFactory() {
        return tapkeyServiceFactory;
    }

}

Polling Data

To allow unlocking during the device has bad or no network connection, it is essential to frequently fetch and cache data.

Tapkey provides an Android BroadcastReceiver that automatically caches such data frequently in background. To enable this feature, PushNotificationReceiver.register() has to be called in Application.onCreate().

public class App extends Application implements TapkeyAppContext{

    private AndroidTapkeyServiceFactory tapkeyServiceFactory;

    @Override
    public void onCreate() {
        super.onCreate();

        this.tapkeyServiceFactory = new TapkeyServiceFactoryBuilder()
            .build(this);

        /*
         * Register PushNotificationReceiver
         *
         * The PushNotificationReceiver polls for notifications from the Tapkey Trust
         * Service. JobId must be a unique Id across the whole app. The default interval
         * of eight hours can be changed.
         */
        PushNotificationReceiver.register(this, tapkeyServiceFactory.getServerClock(), 1,
            PushNotificationReceiver.DEFAULT_INTERVAL);
     }
}