Remote push notifications on Android with RAD Studio XE6

Posted by on in Blogs
In RAD Studio XE6, we introduced support for remote push notifications with our BaaS (Backend as a Service) integration. We include components for Kinvey and Parse, two popular BaaS providers, right out of the box.



Our BAAS framework in RAD Studio XE6 is based on our REST API framework. To enable push notifications on Android, we require Google Cloud Messaging (GCM) support to receive the push notifications.  Parse does not currently support Google Cloud Messaging with their REST API, so if you are looking to use the same push notification service (BaaS) provider across both iOS and Android, I would recommend using Kinvey.

In this tutorial, I am going to show you how to setup and enable push notifications in your C++ and Delphi applications using XE6. The Delphi or C++ code is the same across iOS and Android, but the steps for setting up notifications are different on Android than on iOS, since you are connecting with Google Cloud Messaging (GCM) on Android and with Apple Push Notification (APN) on iOS.

In order to receive push notifications, you need to set up the messaging service (APS or GCM), the device, the cloud service (Kinvey), and your RAD Studio application. We have a great step-by-step tutorial on our docwiki that I recommend you look at since it walks you through all the steps.

My demo consists of a single form with a top aligned Toolbar and Label (to indicate the application name), and a client aligned ListView. The ListView will display each notification as a new item in the list. When the app is running in the background or is closed, you will see the notification displayed in the notification center on your device. On Android, you can access the notification center by swiping down from the top of your screen.

On my form, I placed 2 components:

  • TPushEvent, connected to my KinveyProvider component, with the following event setup:




C++

void __fastcall TForm3::PushEvents1PushReceived(TObject *Sender, const TPushData *AData)

{

ListView1->Items->Add()->Text = AData->Message;

}

Delphi

procedure TForm1.PushEvents1PushReceived(Sender: TObject;
const AData: TPushData);
begin
ListView1.Items.Add.Text = AData.Message;
end;




Next, you will need to setup the Messaging service. Please see this tutorial on how to register with Google and setup your push notification project. As part of that setup, you will be assigned a Project Number that you will need to enter on your Kinvey component, along with your Kinvey account info that you were provided when you signed up on Kinvey.com and setup the project.



BaaS uses OpenSSL, but for Android, those files already exist on the file system, so you don't need to add or link in any ssl library files. To send push notifications, you will need to login to your Kinvey.com account, go to  Addons > Messaging > Push and connect your Kinvey account to your Google Cloud Messaging account:

  1. Copy:

    • the Project ID from your Google Cloud Messaging Setup

    • the API Key from your Google Cloud Messaging Setup


    CCG Configuration.png


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, RAD Studio uses AndroidManifest.template.xml as a template to generate AndroidManifest.xml in the output directory.

You will need to edit the template file which lives in the same folder as your project. You can access it in your project folder (i.e. C:/MyPushDemo) after you have built the project. The entries you need to add are described here.

Now I am going to hit Run in the IDE to deploy the application to my Nexus 10.

To send the push notification to my app, I just had to click on Addons > Messaging > Push and select 'Send a Push'.



Sarina
About
Gold User, Rank: 5, Points: 558
Senior Product Manager, RAD Studio

Comments

  • Paolo Pedrielli
    Paolo Pedrielli Monday, 25 July 2016

    Hi Sarina,
    I've a big problem with remote notification.
    When the app is opened, everything works. When the app is closed, I receive the push notification but, when I click on it, my app opens but it stays to splash screen for a long time and then exit. I tried to check what event is fired but neither OnPushReceived and FormActivate are fired. What's wrong?
    Thanks
    Paolo

  • Steve J
    Steve J Monday, 04 July 2016

    Hi Sarina,
    This looks fine and I was able to get my Kinvey messages pushed to iOS and my Android device.
    The problem is that each time I modify my app, I suppress it from the Android device, deploy it, then it creates a new registration with Kinvey.
    Then, instead of 1 push notification, I get as many notifications as I deployed a new version.
    If I debug my app 15 times and deploy it to my Android tablet, then I will get 15 copies of my notification.
    It seems to be a known behavior. How can we avoid the Android device to register each time? Any way to check the registration and skip it if Kinvey already has the Device ID?
    (I am with Berlin 10.1)
    iOS is fine, it shows only 1 notification.

    Thanks
    Steve

  • Guest
    Asaf Wednesday, 16 April 2014

    Hi,

    If I have for example 1000 registered devices that I want to send push notifications to them how can I do that from my server and from Kinvey or Parse console?

    Thanks,
    Asaf

  • Guest
    sarinadupont Thursday, 17 April 2014

    Hi Asaf,

    If you select Addons > Messaging > Push and select to send a message to Everyone, then everyone who has your app installed will receive that message. You can also setup specific users inside Kinvey and then select 'Specific Users' and define the users you want to send the message to.

    If you want to pass data from your own server to Kinvey or Parse to be sent as a notification, you can setup a custom endpoint.

    Inside your Kinvey account, you would go to Business Logic->Custom Endpoint and define your custom endpoint, i.e. MyCustomMessage.

    Then, on the KinveyProvider component, you would set the PushEndpoint property to the name you defined (i.e. MyCustomMessage) for the Custom Endpoint inside your Kinvey account.

    Then, you need to setup some custom code for your Custom Endpoint inside your Kinvey account (Business Logic->Custom Endpoint), i.e.:

    function onRequest(request, response, modules){
    var iOSAps = request.body.iosaps;
    var push = modules.push;
    var iOSExtras = request.body.iosextras;
    var androidPayload = request.body.androidpayload;
    var androidmessage = androidPayload.message;
    var message = request.body.message;
    push.broadcastPayload(iOSAps, iOSExtras, androidPayload);
    response.complete( 200 );
    }


    Regards,
    Sarina

  • Guest

    [...] time I send a notification to my app (based on the instructions in this blog post), I will see the raw json data returned in my Memo. This is very useful for seeing what json you [...]

  • Guest

    [...] to your mobile devices on iOS and Android. This blog post assumes that you followed the steps in my previous blog post and in the referenced docwiki [...]

  • Guest

    [...] ab. Die ganze Push-Notification-Sache funktioniert gekapselt in einem Testprojekt (abgeguckt von Sarina) Vermutlich stimmt hier was am Ablauf nicht so ganz, weshalb wahrscheinlich noch etwas an der [...]

  • Guest
    Godfrey Fletcher Tuesday, 29 April 2014

    Hi

    Can you send a push notification to one device. I would like to send a message to one of my customer's cell phone. On receiving the message the customer must activate my app(which may not be running at the time)and respond by adding some information. Is this possible?

    Thanks

  • Guest

    [...] I thought I would expand on my recent blog post that covered our new push notification support that we introduced in RAD Studio [...]

  • Guest
    sarinadupont Wednesday, 30 April 2014

    Hi Godfrey,

    You can setup users inside your BaaS provider account and define a username and password for each user. You could then add a login screen to your mobile app, and if the user is logged in to their device, you could send a specific push notification to that user.

    When you define your push notification message inside your BaaS provider account, you can define the user you want to send the message to.

    Regards,
    Sarina

  • Guest
    Agustin Seifert Wednesday, 30 April 2014

    Hi!

    I have to questions.
    First, my app isnt running and the user just press one notification. How can I know which one was pressed? when the app is running pressing the notification fires again "PushReceive" event but when app isnt running not.
    Second, how can I remove a notification from list?

    Thanks in advice,
    Regards!

  • Guest
    sarinadupont Monday, 05 May 2014

    Hi Agustin,

    TPushEvents has a property called StartupNotification. If the application was started when the user selected a notification, the StartupNotification property will contain the notification.

    Regarding removing a notification from the list, that is something the user needs to do by swiping to the left inside notification center to delete the notification or clicking on the icon in the notification center to clear all notifications.

    Regards,
    Sarina

  • Guest
    Luigi Monday, 05 May 2014

    put component notificationcenter...

    procedure TForm1.PushEvents1PushReceived(Sender: TObject;
    const AData: TPushData);
    var Notifica:TNotification;
    begin
    Notifica:=NotificationCenter1.CreateNotification;
    try
    Notifica.EnableSound:=True;
    Notifica.AlertBody:=AData.Message; NotificationCenter1.PresentNotification(Notifica);
    finally
    Notifica.DisposeOf;
    end;

    void __fastcall TForm1::PushEvents1PushReceived(TObject *Sender, const TPushData *AData)
    {
    TNotification * Notifica = NotificationCenter1->CreateNotification();
    Notifica->EnableSound=true;
    Notifica->AlertBody=AData->Message;
    NotificationCenter1->PresentNotification(Notifica);
    Notifica->DisposeOf;

    the original example is from embarcadero help. i change only AData for alert body

    Thank you Sarina for this tutorial

  • Guest
    Agustin Seifert Tuesday, 20 May 2014

    Thanks for your answer Sarina! Works great

  • Guest
    sarinadupont Tuesday, 20 May 2014

    Hi Agustin,

    Glad to hear it worked for you.

    Thanks,
    Sarina

  • Guest
    giuseppe magistro Saturday, 24 May 2014

    Hi,
    i tried to follow your tutorial and is receiving notification inside the app and when the app is stopped.
    when the app is stopped the notification do not has sound alert , how is possible to have sound ,vibration and light flash she notification is received ??
    Thanks
    Beppe

  • Guest
    Peter Monday, 26 May 2014

    Hello everyone,

    I've got a problem with the push notifications.
    everytime I send a push to 'everyone', I got 7 pushnotifications on my smartphone of the same push.

    Do you have any ideas where the problem could be?

    Best regards!

  • Guest

    [...] Sarina DuPont, Product Manager RAD Studio » Remote push notifications on Android with RAD Stud... [...]

  • Guest
    sarinadupont Tuesday, 27 May 2014

    Hi Peter,

    Are you using the same Google Cloud Messaging ID with multiple sample apps installed on the same device? You should use a unique GCM ID for each app on your device.

    Regards,
    Sarina

  • Guest
    sarinadupont Tuesday, 27 May 2014

    Hi Giuseppe,

    Regarding sound files on iOS for push notifications, I would recommend looking at this URL:

    https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/IPhoneOSClientImp.html

    The sound property identifies a sound file in the app bundle.

    Regards,
    Sarina

  • Please login first in order for you to submit comments