Chapter 2. Using RHMAP Features in Cloud Apps

2.1. Developing An Application Using Push Notifications


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 Hosted, 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 from the Push Notification Hello World template

Push Notification Hello World template

  1. In RHMAP Studio, go to Projects and click New Project.
  2. Look for the Push Notification Hello World template, and click Choose on the right-hand side.
  3. Enter a name for the project in the Name your Project field.
  4. In the App Templates section, look for Simple Cordova Push App. Make sure this app is selected and deselect all other apps that can be deselected within the project template. The check boxes are in the top right corner of each app template.
  5. Click Create at the top right of the project template. Wait until the project creation is completed. Then click Finish at the bottom of the screen.

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

Build Client App binary for Android

We will build a Debug type of the Android binary which is easy to build without any special setup.

  1. Click Build on the left-hand side of the screen.
  2. In the Client Binary section, select Android.
  3. Click Build at the bottom of the screen and wait until the app is built. You can monitor progress at the bottom of the screen in the Artifact History section.
  4. Once the build is finished, a dialog with a download link and a QR code should pop up. Alternatively, click Download in the first line of the Artifact History table.
  5. With your mobile device, scan the presented QR code and install the app.

2.1.7. Test the app

Send push notification

  1. On your mobile device, open the newly installed Simple Cordova Push App.
  2. In RHMAP, open your project, and in Apps, Clouds & Services, open the Simple Cordova Push App.
  3. Click Push on the left side of the screen.
  4. Click Send Notification to this app at the top of the screen.
  5. Fill in the Message, keep all other fields unchanged, then click Send Push Notification.
  6. Shortly, your mobile device should receive a push notification with the provided message.

To check logs for the push notification, navigate to the Client App in Studio and click Push in the left-hand side of the screen, then choose the Activity Log tab. The log includes the following columns:

  • Message - the text of the notification
  • IP Address - the IP address of the cloud app that issued the push message request
  • Installations - the number of clients that opened the notification
  • Status - the status of the notification, 'Processed' means the notification has been received by * the Notification Service, 'Failed' means the notification has not been processed by the * Notification Service
  • Sent - the timestamp for the notification leaving RHMAP
  • First time opened - the timestamp for the first opening of the notification
  • Last time opened - the timestamp for the most recent opening of the notification

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:

$'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.