Adding the Persistent Mobile Foundation SDK to Android Applications

Overview

The Persistent Mobile Foundation SDK consists of a collection of dependencies that are available through Maven Central, and which you can add to your Android Studio project. The dependencies correspond to core functions and other functions:

  • PersistentMobileFoundation - Implements client-to-server connectivity, handles authentication and security aspects, resource requests, and other required core functions.
  • PersistentMobileFoundationJSONStore - Contains the JSONStore framework. For more information, review the JSONStore for Andoid tutorial.
  • PersistentMobileFoundationPush - Contains the Push Notifications framework. For more information, review the Notifications tutorials.

In this tutorial, you learn how to add the PMF Native SDK by using Gradle to a new or existing Android application. You also learn how to configure the PMF to recognize the application, and to find information about the PMF configuration files that are added to the project.

Prerequisites:

Jump to:

Adding the PMF Native SDK

Follow the instructions below to add the PMF Native SDK to a new or existing Android Studio project, and to register the application to the PMF instance.

Before you start, make sure that PMF is running.
If you use a locally installed server: From a Command-line window, navigate to the server’s folder and run the command ./run.sh on a Mac or Linux OS, or run.cmd on Windows.

Creating an Android application

Create an Android Studio project or use an existing one.

Adding the PMF Native SDK

You can add the PMF SDK by following below instructions:

To add the PMF SDK, first download the SDK .zip file from the PMF Operations Console → Download Center → SDKs tab. After completing the above steps, follow the below as well.

  • Extract the downloaded .zip file and place the relevant aar files to the app\libs folder.
  • Add the following to the dependencies closure:
    implementation fileTree(include: ['*.aar'], dir: 'libs')
    implementation 'com.squareup.okhttp3:okhttp-urlconnection:3.4.1'   
    implementation 'com.squareup.okhttp3:okhttp:3.4.1'
  • Add the following to the repositories closure:
    repositories {
        flatDir {
            dirs 'libs'
        }
    }
    
    
    > If a Gradle Sync request appears, accept it.

In Android → app → manifests, open the AndroidManifest.xml file. Add the following permissions above the application element:

   <uses-permission android:name="android.permission.INTERNET"/>
   <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

Add the PMF UI activity next to the existing activity element:

   <activity android:name="com.worklight.wlclient.ui.UIActivity" />

Registering the application

  1. Open a Command-line window and navigate to the root of the Android Studio project.

  2. Run the command:

     pmfdev app register
    

The pmfdev app register CLI command first connects to the PMF to register the application, followed by generating the mfpclient.properties file in the [project root]/app/src/main/assets/ folder of the Android Studio project, and to add to it the metadata that identifies the PMF.

Tip: You can also register applications from the PMF Operations Console:

  1. Load the PMF Operations Console.
  2. Click the New button next to Applications to register a new application and follow the on-screen instructions.
  3. After the application is registered, navigate to the application’s Configuration Files tab and copy or download the mfpclient.properties file. Follow the onscreen instructions to add the file to your project.

Creating a WLClient instance

Before using any PMF APIs, create a WLClient instance:

WLClient.createInstance(this);

Note: Creating a WLClient instance should happen only once in the entire application lifecycle. It is recommended to use the Android Application class to do it.

Updating the PMF Native SDK

To update the PMF Native SDK with the latest release, find the release version number and update the version property accordingly in the build.gradle file.

Generated PMF Native SDK artifacts

mfpclient.properties

Located in the ./app/src/main/assets/ folder of the Android Studio project, this file defines the client-side properties used for registering your Android app on the PMF.

Property Description Example values
wlServerProtocol The communication protocol with the PMF. http or https
wlServerHost The host name of the PMF. 192.168.1.63
wlServerPort The port of the PMF. 9080
wlServerContext The context root path of the application on the PMF. /mfp/
languagePreferences Sets the default language for client sdk system messages. en

Tutorials to follow next

With the PMF Native SDK now integrated, you can now:

Last modified on