Using Custom Endpoints to create a BaaS enabled Desktop application for sending notifications to your mobile apps

Posted by on in Cloud

I previously wrote a blog post on how to send notifications via our BaaS (Backend as a Service) support in Delphi 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 articles.

In this post, I thought I would cover how to send notifications from BaaS enabled FireMonkey or VCL Desktop apps to your mobile applications. My previous blog post covered how to send notifications to your mobile application using Kinvey's web interface, but this allows you to create a FireMonkey or VCL Desktop application to send notifications to your mobile application.

Your Desktop application will connect with Kinvey in this case, then pass the message to Google's Cloud Messaging service which then sends the notification to your mobile application.

If you want to pass a message from your Desktop app to Kinvey or Parse to be sent as a notification in your mobile client application, you can setup a custom endpoint. Inside your Kinvey account, go to Business Logic->Custom Endpoint and define your custom endpoint, i.e. MyMessage.



Next, you need to setup some custom code for your Custom 'MyMessage' 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 );
}




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



On the Desktop app, I have a BackendPush component hooked into my Kinveyprovider component:

I also have a button on the form of my Desktop application that has the following event setup to send the notification containing the GCM title and GCM message from the Edit controls:

procedure TForm30.SendMessageClick(Sender: TObject);

begin

BackendPush1.Push;

end;






I then connected my Edit controls to the BackendPush members for Google Cloud Messaging (GCM) via the LiveBindings Designer (View->LiveBindings Designer).



The Message property can be used across iOS and Android whereas the APS (iOS) and GCM (Android) fields are platform specific.

Now, messages I send from my Desktop app get sent to my mobile application.

For my mobile application, if I wanted to display both the message title and description in a ListView as shown in my previous blog post, I would have to update my PushEventsReceived event:

procedure TForm30.PushEvents1PushReceived(Sender: TObject;

const AData: TPushData);

begin

ListView1.Items.Add.Text = AData.GCM.Title;

ListView1.Items.Add.Detail =  AData.Message;

end;


Sarina



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

Comments

  • Paolo Pedrielli
    Paolo Pedrielli Thursday, 14 July 2016

    Hi Sarina,
    in Delphi Seattle there is also a 'target' property in TBackEndPush component, but it seems not working with Kinvey. Is it possible?

  • Sarina D
    Sarina D Thursday, 14 July 2016

    Hi Paolo,
    What are you trying to do?

    Regards,
    Sarina

  • Paolo Pedrielli
    Paolo Pedrielli Thursday, 14 July 2016

    Hi Sarina,
    I'm trying to send a push notification only to a specific user, configured in Kinvey.
    TBackEndPush says me this operation is not supported (I think it was supported on Parse, searching on Internet I've found some samples).
    I followed your instruction on how to user TKinveyAPI but, using SendPayLoad either with an array of string containing Users, and also with a TUser parameter, but it doesn't work because TJSONObject is not correct, the constraint "sUsersElement = 'users'" is ok but the pair "username":"" doesn't fit correctly.
    So I create a derived class of TKinveyAPI and I override the SandPayLoad with something like that:

    x.AddPair('username','myusername');
    LJSONObject.AddPair('users', x as TJSONObject);

    And finally I got it! Also changed my backend with a push.SendPayLoad instead of push.BroadcastPayLoad, here the code:

    function onRequest(request, response, modules){
    var users = request.body.users;
    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;
    var collectionAccess = modules.collectionAccess;

    collectionAccess.collection('user').find(users, function (err, userColl) {
    if (err)
    {
    } else {
    userColl.forEach(function (user) {
    push.sendPayload(user, iOSAps, iOSExtras, androidPayload);
    });
    }
    response.complete( 200 );
    });
    }

    For a single user, it working like a charm! I don't know how to pass a list of users, either in TJSon and in BackEnd because I don't know the syntax of collecion.Find to search for a list of elements, but it doesn't matter, for now.

    Thank you very much.

    Regards,
    Paolo

  • Goran E9134
    Goran E9134 Wednesday, 9 March 2016

    All images are gone (IE11, Win10) when I access this at March-09-16

  • Guest
    Flavio Basile Thursday, 24 April 2014

    Hi Sarina,
    thanks for yours posts: very interesting!

    It is possible to have BaaS components for XE5?
    Or Push notification can by used also whit XE5?

  • Guest
    Sarina DuPont Wednesday, 30 April 2014

    Hi Flavio,

    Thanks, glad you liked the post. BaaS is a new feature introduced in XE6, including our integrated push notification support.

    Regards,
    Sarina

  • Guest
    Oliver Schulte Monday, 12 May 2014

    Hi Sarina,

    great work, thank you.

    When sending push-notifications via Kinvey website or backendpush-component there is no sound - iOS and Android devices are remaining silent. Is there a way to make a sound? How can I use the APS.sound property?

    Regards
    Oliver

  • Guest
    Rob Jongejan Monday, 19 May 2014

    Hello Sarina,
    Thanks for the explanation, but is it also possible, like in the Kinvey website, to send a message to a specific user?

  • Guest
    sarinadupont Tuesday, 20 May 2014

    Hi Oliver,

    According to Apple's documentation (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

  • Guest
    Connor Webster Wednesday, 25 June 2014

    Hi Sarina,

    Great tutorials, very concise.

    I'm just wondering if there is a way to target specific users when sending push notifications from a custom endpoint within a windows application; rather then sending the notification to all users, either through a BaaS component or through another method?

    Regards,
    Connor

  • Guest
    sarinadupont Friday, 25 July 2014

    Hi Connor,

    The TBackendPush component does not have a method to push notify specific users. So you will need to use TKinveyAPI.

    procedure TKinveyApi.SendPayload(const AEndpointName: string;
    AUsers: array of string; const AIOSAps, AIOSExtras,
    AAndroidPayload: TJSONObject);

    This call will pass a JSON object to a custom endpoint. The JSON object will have an element called users. Users will be an array of User IDs. You will need to have code in the custom endpoint that does something with the users array.

    You can create an instance of TKinveyAPI as follows:

    FApi := TKinveyAPI.Create(nil);
    // set connection properties
    KinveyProvider1.UpdateApi(FApi);

    An alternative is to get the TKinveyAPI instance from the TBackendPush component.
    TBackendPush has its own instance of TKinveyAPI that it uses to call Kinvey. The advantage of using the one from BackendPush1, rather than create a new TKinveyAPI, is that you don't need to set the connection properties.

    The following code is used to access this instance.

    var
    LApi: TKinveyAPI;
    LIntf: IGetKinveyAPI;
    begin
    if Supports(BackendPush1.PushAPI.ProviderAPI, IGetKinveyApi, LIntf) then
    LApi := LIntf.GetKinveyApi;


    Regards,
    Sarina

  • Please login first in order for you to submit comments
  • Page :
  • 1

Check out more tips and tricks in this development video: