Show: Object Pascal C++
Display Preferences

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.
    Link.png
  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)
      KinveyProvider.png
      In the Object Inspector, set the following properties of the KinveyProvider:
      KnvProv.png
    2. Using TParseProvider (iOS only)
      ParseProvider.png
      In the Object Inspector, set the following properties of the ParseProvider:
      ParsePorviderSett.png

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.
    PushEvents.png
  2. Switch to Code tab by pressing F12.
  3. Define the event handlers as follows:
    Object Pascal:
    implementation
     
    {$R *.fmx}
     
    procedure TForm1.PushEvents1DeviceRegistered(Sender: TObject);
    begin
      Memo1.Lines.Add('Device Registered');
      Memo1.Lines.Add('');
    end;
     
    procedure TForm1.PushEvents1DeviceTokenReceived(Sender: TObject);
    begin
      Memo1.Lines.Add('Device Token Received');
      Memo1.Lines.Add('');
    end;
     
    procedure TForm1.PushEvents1DeviceTokenRequestFailed(Sender: TObject;
      const AErrorMessage: string);
    begin
      Memo1.Lines.Add('Device Token Request Failed');
      Memo1.Lines.Add(AErrorMessage);
      Memo1.Lines.Add('');
    end;
     
    procedure TForm1.PushEvents1PushReceived(Sender: TObject;
      const AData: TPushData);
    begin
      Memo1.Lines.Add('Device push received');
      Memo1.Lines.Add(AData.Message);
      Memo1.Lines.Add('');
    end;
     
    end.
    C++:
    //--------------------------------------------------------------------------
    void __fastcall TForm1::PushEvents1DeviceRegistered(TObject *Sender)
    {
       Memo1->Lines->Add("Device Registered");
       Memo1->Lines->Add("");
    }
    //--------------------------------------------------------------------------
    void __fastcall TForm1::PushEvents1DeviceTokenReceived(TObject *Sender)
    {
       Memo1->Lines->Add("Device Token Received");
       Memo1->Lines->Add("");
    }
    //--------------------------------------------------------------------------
    void __fastcall TForm1::PushEvents1DeviceTokenRequestFailed(TObject *Sender, const UnicodeString AErrorMessage)
     
    {
      Memo1->Lines->Add("Device Token Request Failed");
      Memo1->Lines->Add(AErrorMessage);
      Memo1->Lines->Add("");
    }
    //--------------------------------------------------------------------------
    void __fastcall TForm1::PushEvents1PushReceived(TObject *Sender, const TPushData *AData)
     
    {
      Memo1->Lines->Add("Push Received");
      Memo1->Lines->Add(AData->Message);
      Memo1->Lines->Add("");
    }
    //--------------------------------------------------------------------------

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:
<%uses-permisson%>

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. -->
    <uses-permission
    android:name="com.google.android.c2dm.permission.RECEIVE" />

Registering the Receiver Class

Search for this block:

</intent-filter> 
        </activity>

Add the following code:

<receiver
      android:name="com.embarcadero.gcm.notifications.GCMNotification" android:exported="true"
      android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
          <action android:name="com.google.android.c2dm.intent.RECEIVE" />
          <category android:name="%package%" />
        </intent-filter>
      </receiver>

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

OpenSSL

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.

Libraries.png

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).
VersionInfo.png

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:
    Parse

    ParseHello.png

    Kinvey

    HelloKinvey.png

  5. Switch to your mobile device:
    iOS

    IosDevice.png

    Android

    AndroidHello.png


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

    Pushios.png

    Android

    AndNotificationCenter.png

Previous

See Also

Personal tools
In other languages