Tracking user location with offline-enabled mobile apps
This tutorial describes how to configure your offline-enabled app to record and synchronize device location data. The Pega Mobile Client™ API set contains a Location Recorder API, which allows offline-enabled mobile apps to record and store geolocation information about a mobile device. Collected while the device is disconnected from the network, this information can later, after the device regains connectivity, be transferred to Pega Platform™ to provide aggregated information about where a user has been. For example, you can use this feature to record work time and the route of a delivery truck traveling in areas with poor network coverage.
As a Pega Platform application administrator, you can customize your application to use this recorded location information. You configure and initiate the location recording feature at the Pega Platform application level. You can configure the recording frequency, as well as data synchronization frequency. Synchronized data is then passed through a customizable activity and stored in the database.
Prerequisites
Before you start this tutorial, you must meet the following requirements:
- Be familiar with the Pega Mobile Client API set. For more information, see Pega Mobile Client API reference.
- Know how to use the Pega Mobile Client API. For more information see Searching device contacts with Pega Mobile Client API.
- Be an experienced JavaScript programmer.
- Have thorough knowledge and experience creating Pega Platform applications.
- Know how to create an offline-enabled mobile apps. For more information, see Configuring offline support for a mobile app.
Setting up the Location Recorder API
To create, configure, and initiate a recorder from within a Pega Platform application, you must use the Location Recorder API.
- Right-click and download the following file that contains a JavaScript application that uses the API:
LocationRecorderExample.zip (0.1 MB)
Review the contents of the JavaScript application to learn how to configure and initiate the API.
Create a custom JavaScript that makes use of the API and associate it with a control in your Pega Platform application, for example, a button.
Configuring the activity rule
After you create and modify your Pega Platform application, clone the pyProcessGeolocationData activity rule to enable data capture.
- Search for the pyProcessGeolocationData activity rule by using the search text field in Designer Studio.
- Click Save As >Specialize by class or ruleset to create a clone of the rule with the same name.
- Specify the context by selecting the @baseclass class in the Apply to field in your application ruleset, and click .
- When the activity rule opens, in the Steps tab, click the right angle bracket (>) to display the Java code behind the rule, similar to the following code:
String recorderID = tools.getParamValue("recorderID");
oLog.infoForced("recorderID: " + recorderID);
ParameterPage parametersPage = tools.getParameterPage();
Object locationsJSON = parametersPage.getParameterValue("locationsJson");
org.json.simple.JSONArray json = (org.json.simple.JSONArray)locationsJSON;
Iterator locationsBatch = json.iterator();
while (locationsBatch.hasNext()) {
org.json.simple.JSONObject batch = (org.json.simple.JSONObject)locationsBatch.next();
oLog.infoForced("attributes: " + batch.get("attributes"));
oLog.infoForced("columns: " + batch.get("columns"));
org.json.simple.JSONArray locationsArray = (org.json.simple.JSONArray)batch.get("locations");
Iterator locations = locationsArray.iterator();
while (locations.hasNext()) {
oLog.infoForced(" location: " + locations.next());
}
} - The existing code writes the collected geolocation data to system logs. You can modify the code according to your needs.
- Click Save to apply your changes to the pyProcessGeolocationData activity rule.