Creating the Application

From Appmethod Topics
Jump to: navigation, search

Go Up to Mobile Tutorial: Using Remote Notifications (iOS and Android)

In this step, you create the basic application with elements that are generic to Android and iOS, and you add the code that makes the characteristics for the specific platforms.

Note: Because our implementation is based on the REST BAAS framework, Appmethod allows you to use:
  • Either provider (Kinvey or Parse) with iOS apps
  • Only Kinvey with Android apps

Design and Set Up the User Interface

  1. To create an HD FireMonkey Mobile Application, select either of the following:
    File > New > FireMonkey Mobile Application - Object Pascal > Blank Application
    File > New > FireMonkey Mobile Application - C++ > Blank Application
  2. Drop a TCheckBox component on the form.
    In the Object Inspector, set the following properties of the CheckBox:
    1. Set the Align to Top.
    2. Set the IsChecked property to False.
    3. Set the Text to Active.
  3. Add a TMemo component to the form and set the Align property to Client.
  4. Drop a TPushEvents object.
  5. Set the AutoActivate to False.
  6. In LiveBindings Designer add a link from Active in PushEvents to IsChecked in CheckBox. The Active property of PushEvents is set to True when the CheckBox component is checked.
  7. Depending on the Cloud Service you want to use, add a TParseProvider for Parse or TKinveyProvider for Kinvey.

    Note: This tutorial uses both of these cloud services as follows:

    • Kinvey for Android devices
    • Parse for iOS devices
    1. Using TKinveyProvider (iOS or Android)
      In the Object Inspector, set the following properties of the KinveyProvider:
    2. Using TParseProvider (iOS only)
      In the Object Inspector, set the following properties of the ParseProvider:

Creating the Event Handlers

  1. On the form, select PushEvent1 and go to the Object Inspector:
    • Check whether the Provider property is set to KinveyProvider1 or to ParseProvider1, depending on which provider you used.
    • Go to Events tab and create an event handler for each event by double-clicking the Value field.
  2. Switch to Code tab by pressing F12.
  3. Define the event handlers as follows:
    Object Pascal:
    {$R *.fmx}
    procedure TForm1.PushEvents1DeviceRegistered(Sender: TObject);
      Memo1.Lines.Add('Device Registered');
    procedure TForm1.PushEvents1DeviceTokenReceived(Sender: TObject);
      Memo1.Lines.Add('Device Token Received');
    procedure TForm1.PushEvents1DeviceTokenRequestFailed(Sender: TObject;
      const AErrorMessage: string);
      Memo1.Lines.Add('Device Token Request Failed');
    procedure TForm1.PushEvents1PushReceived(Sender: TObject;
      const AData: TPushData);
      Memo1.Lines.Add('Device push received');
    void __fastcall TForm1::PushEvents1DeviceRegistered(TObject *Sender)
       Memo1->Lines->Add("Device Registered");
    void __fastcall TForm1::PushEvents1DeviceTokenReceived(TObject *Sender)
       Memo1->Lines->Add("Device Token Received");
    void __fastcall TForm1::PushEvents1DeviceTokenRequestFailed(TObject *Sender, const UnicodeString AErrorMessage)
      Memo1->Lines->Add("Device Token Request Failed");
    void __fastcall TForm1::PushEvents1PushReceived(TObject *Sender, const TPushData *AData)
      Memo1->Lines->Add("Push Received");

Android Settings

Note: To verify whether your Android device supports GCM, see GCM Overview

Editing AndroidManifest.template.xml

To enable GCM support in an Android application you will need to include some additional entries in the AndroidManifest.xml for the project. When you build your project, Appmethod uses AndroidManifest.template.xml as a template to generate AndroidManifest.xml in the output directory.

Adding the Permissions

Some of the necessary permissions can be enabled in the project options, but it is easier just to copy and insert the whole block including all permissions needed for GCM.

In the AndroidManifest.template.xml file search for a template variable:


Insert the following lines directly below:

<!-- GCM connects to Google Services. -->
    <uses-permission android:name="android.permission.INTERNET" />

    <!-- GCM requires a Google account. -->
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />

    <!-- Keeps the processor from sleeping when a message is received. -->
    <uses-permission android:name="android.permission.WAKE_LOCK" />

     Creates a custom permission so only this app can receive its messages.

     NOTE: the permission *must* be called PACKAGE.permission.C2D_MESSAGE,
     where PACKAGE is the application's package name.
    <permission android:name="%package%.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />
    <uses-permission android:name="%package%.permission.C2D_MESSAGE" />

    <!-- This app has permission to register and receive data message. -->
    android:name="" />

Registering the Receiver Class

Search for this block:


Add the following code:

      android:name="com.embarcadero.gcm.notifications.GCMNotification" android:exported="true"
      android:permission="" >
          <action android:name="" />
          <category android:name="%package%" />

Registering the Service Class

This is a Java class that will create an entry in the Notification Center of the Android Device - even if the application itself is not running while the remote notification comes in. If you don’t need or want items in the Notification Center, you can skip this step.

Add the following code below the previous block from Registering the Receiver Class:

<service android:name="com.embarcadero.gcm.notifications.GCMIntentService" />

iOS Settings


The REST API of Kinvey and Parse are accessed through secure http (HTTPS). Your application will need OpenSSL support in order to make HTTPS requests. On platforms that do not include OpenSSL libraries, such as iOS, you will need to download OpenSSL libraries.

For iOS devices you need to download the libcrypto.a and libssl.a static libraries.

The location of downloaded libraries is important. For static libraries, the files need to be in the linker library path or in the project directory.


Project Settings

  1. Right-click your project in the Project Manager. Choose Project > Options > Entitlement List, and select Receive Push Notification in iOS.
  2. Choose Project > Options > Version Info, and set the CFBundleIdentifier key. This should be the same as the identifier of your App ID. It is the Bundle ID from step 2 in this tutorial (Creating iOS App ID on Apple Developer Program section).

Running Your Application on a Mobile Device

Now your application is ready to run on either a simulator or your connected mobile device.
To run your application

  1. In Project Manager, select your target platform.
  2. Choose either of the following commands:
    • Run > Run
    • Run > Run Without Debugging
  3. Click the Active checkbox.
  4. Go to Parse or Kinvey and send a new push:




  5. Switch to your mobile device:




  6. Send your app to background and send another push from Parse or Kinvey. Now go to Notifications:





See Also