Chapter 2. Using RHMAP Features in Cloud Apps

2.1. Developing An Application Using Push Notifications

Overview

This tutorial will guide you through the process of building a sample application which receives push notifications sent from the Platform’s built-in push notification server. The application you’ll create in this example is based on Cordova and targets Android and the associated Firebase Cloud Messaging (formerly Google Cloud Messaging) platform. However, the steps are analogous for all other supported platforms.

To learn more about push notification support in the Red Hat Mobile Application Platform, see Push Notifications.

2.1.1. Obtain Firebase Cloud Messaging credentials and download the google-services.json file

For the built-in UnifiedPush Server (UPS) to be able to access Google’s push notification network, you first need to get a Server key and establish a project in the Firebase Console. See Obtaining Firebase Cloud Messaging Credentials for a detailed walk-through of getting the credentials. These instructions are for a native Android app, you do not need to complete all the process. In order to successfully complete the setup you need to obtain the following three items:

  • Server key (formerly API key)
  • Sender ID (formerly Project Number)
  • the google-services.json file generated from the Firebase console

For other push networks, see Obtaining Credentials for Push Networks.

2.1.2. Create a project for your Push Notification app

Create a new project for your Push Notification app. Use the Push Notification templates as a reference.

2.1.3. Set up push support

Push support needs to be explicitly enabled for each Client App and you also need to provide the push network credentials obtained in the first step Obtain Firebase Cloud Messaging credentials and download the google-services.json file.

Add Android variant

  1. In the Apps, Cloud Apps & Services section of your project, click the Simple Cordova Push App.
  2. Click Push on the left side of the screen.
  3. Click Enable Push.
  4. Select the Android option and enter the Server key and Sender ID obtained in the first step. Click Enable Push.

2.1.4. Integrate the google-services.json file into the Client App

  1. Click Editor on the left-hand side of the screen.
  2. Select the www folder in the Editor, click the + symbol in the toolbar to create a new file, and name it google-services.json.
  3. Open the google-services.json file you have previously downloaded from the Firebase Console, copy the contents into the google-services.json in the Editor.
  4. Click File, and then Save in the Editor toolbar to save your changes.

2.1.5. Configure config.xml file

Modify the widget id setting in the config.xml file to correspond to the package name defined in the Firebase Console.

2.1.6. Build the Client App binary

Refer to Build the Client App binary.

2.1.7. Test the app

Refer to Test the App.

2.2. Dynamically Populating Form Fields From an MBaaS Service

2.2.1. Overview

You can use an endpoint of an MBaaS service as the data source for a form field in the Forms Builder. This guide shows you how to create the MBaaS service, define the data source, and populate a form field using the data source.

See Using Data Sources for more information on data sources for Forms.

2.2.2. Creating a Service for a Data Source

To define a data source, you must provide an MBaaS service endpoint which serves valid JSON responses for the data source. In this example, you’ll create a new MBaaS service which serves static data from a JSON file.

  1. In the Studio, navigate to Services & APIs. Click Provision MBaaS Service/API.
  2. Click Import Existing Service, provide a name and click next
  3. Select Public Git Repository when the Import From option is expanded
  4. Choose the URL of one of the repositories available here and paste it into Git URL textfield
  5. Click Next
  6. From the left naavigation bar select Deploy and click Deploy Cloud App to deploy your service

2.2.3. Defining a Data Source

Once you’ve created the MBaaS service providing the data, you can now use it in a data source.

  1. In the Studio, navigate to Drag & Drop Apps > Data Sources. Click New Data Source.
  2. Set a name and a description. Both are required.
  3. Choose the previously created MBaaS service and its endpoint to use as the data source.

    The Static Data Source template used in this example serves data at the endpoint /static_ds/months.

    You can check, whether the service returns data valid for use as a data source, using the Validate button. Before the validation, make sure the correct environment is selected using the environment selector in the top right-hand corner.

    If the data is valid, you’ll see the message "Success Data Source Is Valid". If the data is not valid, or the service can not be reached, an error message will indicate the problem.

    After a successful validation, you can view the returned data using the View button.

    Choose a service and schedule

  4. Choose an update frequency – how often the configured MBaaS service endpoint will be called to obtain new data. The update frequency is a value in the range of 1 minute to 7 days.
  5. Choose how many responses from calls to the MBaaS service endpoint should be kept in the audit log.
  6. Click Create Data Source. You will see the message Data Source Created Successfully.

Now, the data source can be used in the Forms Builder to populate a form field.

2.2.4. Using a Data Source in a Form Field

After defining the data source, you can now use it in the Forms Builder.

  1. In the Studio, navigate to Drag & Drop Apps > Forms Builder.
  2. Open an existing form, or create a new form. Navigate to Edit Form.
  3. Drag one of the supported types into the form – for example, a Dropdown field. Select the created field.
  4. In the Options section, check Use a Data Source to populate field options?.
  5. Select the data source created in the previous step.

    Select a data source

    If a selected data source has loaded data at least once before – for example, while validating during creation – the last available version of the data will be displayed in the field options, as a preview.

    Use the environment selector in the top right-hand corner to ensure that the same environment is selected, where the MBaaS service of the data source is deployed.

  6. Save and deploy the form.

If you navigate to the Dashboard, you can now see the field populated by data from the data source in the preview on the right.

2.3. Adding Stats to your App

2.3.1. Overview

The Platform maintains counters and timers that can be accessed by the user. Some are provided by the platform, while others can be specified by the developers of an application.

Counters can be used to track the usage of particular functionality, counters can be incremented and decremented. The platform provides counters that show currently active server-side actions, that is, the number of server-side functions that have been called by a mobile application that are currently in progress.

Timers can be used to track the length of time taken by specified actions. The platform provides timers to track the execution-time of the server-side actions. Periodically, at a platform-configured interval, the current counters, and timers and pushed to a history and zeroed.

When requesting statistics from the platform, the number of recent intervals to return data for, and the length of the flush interval, in milliseconds, is returned with the data.

Timer data is returned as the number of timing events that have occurred in the interval, with upper and lower values, there are also upper and mean values for the 90th percentile.

2.3.2. Creating Counters and Timers

App developers can create their own counters and timers using:

$fh.stats.inc('COUNTERNAME'); // Increments a counter
$fh.stats.dec('COUNTERNAME'); // Decrements a counter
$fh.stats.timing('TIMERNAME', timeInMillis); // Store a timer value

For more detailed documentation about using the Stats API in your Cloud App, see:

Developers' counters and timers can be requested from the platform, by specifying the statstype of "app”.

The keynames of the counters are of the form DOMAIN_APPID_app_COUNTERNAME where DOMAIN_APPID is the name of the domain and APPID is the ID of the app, and COUNTERNAME is the developer supplied name for the counter.

The keynames of the timers are of the form DOMAIN_APPID_app_TIMERNAME where DOMAIN_APPID is the name of the domain and APPID is the ID of the app, and TIMERNAME is the developer supplied name for the timer.

2.3.3. Viewing Stats

A user can access stats data through the Studio or the raw data itself through FHC, the command line client.