Show: Delphi C++
Display Preferences

Multi-Device Application to Receive Push Notifications

From RAD Studio
Jump to: navigation, search

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

You create a basic application to receive push notification 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, RAD Studio allows you to use in your iOS or Android apps one of the following service providers:

Before you create your application to receive push notifications, ensure to complete these two required steps:

Design and Set Up the User Interface

  1. To create an Multi-Device Application, select either of the following:
    File > New > Multi-device Application - Delphi > Blank Application
    File > New > Multi-device Application - C++Builder > 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 service provider you want to use, add:
    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 or Android)
      ParseProvider.png
      In the Object Inspector, set the following properties of the ParseProvider:
      Note: You do not need to add the GCM AppID when developing Android apps and using Parse.
      ParsePorviderSett.png
    3. Using TEMSProvider (iOS or Android)
      EMSProvider.png
      In the Object Inspector, set the following properties of the EMSProvider:
      EMSProv.png

Creating the Event Handlers

  1. On the form, select PushEvents1 and go to the Object Inspector:
    • Check whether the Provider property is set to KinveyProvider1, to ParseProvider1 or to EMSProvider1 , 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:
    Delphi:
    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, TPushData * const AData)
    
    {
      Memo1->Lines->Add("Push Received");
      Memo1->Lines->Add(AData->Message);
      Memo1->Lines->Add("");
    }
    //--------------------------------------------------------------------------
    

Android Settings

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

Project Settings

To enable your application to receive remote notifications:

  1. Right-click your project in the Project Manager.
  2. Choose Project > Options > Entitlement List.
  3. Set Receive Push Notification value to True.


Note: If you want to enable receiving a notification even if the application is not running while the remote notification comes in, you will need to register a Service Class. This Java service class will create an entry in the Notification Center of the Android Device. If you do not need or want items in the Notification Center, you can skip this step.

You need to include an additional entry in the AndroidManifest.xml for the project, by customizing your AndroidManifest.template.xml. In the AndroidManifest.template.xml file of your project search for the following tag:

<%receivers%>

Add the following code below it:

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

Note: Please consider the following updates in your code:

  • Before applying your project settings, you need to register the GCM on a background thread.
    • You can assign the 'TPushEvents.Active' property on the main thread without raising an instance of the 'IOException' Java exception.
    • You can assign the 'TPushServiceConnection.Active' property on the main thread without raising an instance of the 'IOException' Java exception.
  • The state changing to 'Starting' is now supported.
    • 'Android' and 'iOS' platforms started to behave similarly regarding the push service's lifecycle.
    • Now, you can test if the 'push service' entered in the 'Starting' state when a connection change happens, similar to the following code approach:
procedure TForm1.OnServiceConnectionChange(Sender: TObject; PushChanges: TPushService.TChanges);
begin
  if TPushService.TChange.Status in PushChanges then
  begin
    if FPushService.Status = TPushService.TStatus.Starting then
    begin
      Memo1.Lines.Add('GCM registration is going to start.'); 
    end;
  end;
end;
  • Event-handlers are now called on the main thread.
    • The 'TPushEvents.OnDeviceRegistered', 'TPushEvents.OnDeviceTokenReceived', 'TPushEvents.OnDeviceTokenRequestFailed' and 'TPushEvents.OnPushReceived' event-handlers no longer need to use the 'TThread.Synchronize' when interacting with the visual controls.
    • The 'TPushServiceConnection.OnChange' and 'TPushServiceConnection.OnReceiveNotification' event-handlers no longer need to use the 'TThread.Synchronize' when interacting with the visual controls.

iOS Settings

Project Settings

  1. Right-click your project in the Project Manager.
  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 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 Projects Window, 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, Kinvey or Sending RAD Server Push Messages and send a new push:
    Note: You can use your own RAD Server application to send RAD Server Push Notifications messages.
    Parse

    ParseHello.png

    Kinvey

    HelloKinvey.png

    RAD Server (EMS)

    HelloEMS.png

  5. Switch to your mobile device:
    iOS

    IosDevice.png

    Android

    AndroidHello.png

    iOS RAD Server (EMS)

    IOSEMSHello.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

See Also

Code Samples

Personal tools
In other languages
Previous Versions
Assistance