Multi-Device Application to Receive Push Notifications
Go Up to Mobile Tutorial: Using Remote Notifications (iOS and Android)
Contents
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:
- Kinvey
- Parse
- EMS
Before you create your application to receive push notifications, ensure to complete these two required steps:
- Setting Up the Messaging Service
- Choose the provider:
Design and Set Up the User Interface
- 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
- Drop a TCheckBox component on the form.
- In the Object Inspector, set the following properties of the CheckBox:
- Add a TMemo component to the form and set the Align property to Client.
- Drop a TPushEvents object.
- Set the AutoActivate to False.
- 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.
- Depending on the service provider you want to use, add:
- For Parse: a TParseProvider.
- For Kinvey: a TKinveyProvider.
- For EMS: a TEMSProvider.
-
Using TKinveyProvider (iOS or Android)
In the Object Inspector, set the following properties of the KinveyProvider:
- Expand the Android Push node and copy to GCMAppID the Project Number from Registering with Google.
- Set the AppKey, AppSecret and MasterSecret to API Keys values from setting up your project in Kinvey.
- Set UserName and Password to the values defined at Adding an User in Kinvey.
-
Using TParseProvider (iOS or Android)
In the Object Inspector, set the following properties of the ParseProvider:
- Set the ApplicationID, MasterKey and RestApiKey to API Keys from Creating a Project at Parse.
-
Using TEMSProvider (iOS or Android)
In the Object Inspector, set the following properties of the EMSProvider:
- Expand the Android Push node and copy to GCMAppID the Project Number from Registering with Google.
- Set the ApplicationId, AppSecret and MasterSecret to API Keys values from setting up your EMS Server.
Creating the Event Handlers
-
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.
- Switch to Code tab by pressing F12.
- 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 whether your Android device supports GCM, see GCM Overview
Project Settings
To enable your application to receive remote notifications:
- Right-click your project in the Project Manager.
- Choose Project > Options > Entitlement List.
- 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" />
iOS Settings
Project Settings
- Right-click your project in the Project Manager.
- 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.
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
- In Project Manager, select your target platform.
- Choose either of the following commands:
- Run > Run
- Run > Run Without Debugging
- Click the Active checkbox.
- Go to Parse, Kinvey or Sending EMS Push Messages and send a new push:
- Note: You can use your own EMS application to send EMS Push Notifications messages.
Parse Kinvey EMS - Switch to your mobile device:
iOS Android iOS EMS
- Send your app to background and send another push from Parse or Kinvey. Now go to Notifications:
iOS Android
See Also
- Mobile Tutorial: Using Notifications (iOS and Android)
- Mobile Tutorial: Using BaaS for Backend Storage (iOS and Android)
- Enterprise Mobility Services
- EMS Push Notifications
- Entitlement List