I've got an android app that I am thinking about porting to Delphi but I can't see a way to interface with GCM. I am thinking I would possibly have to run the GCMBaseIntentService in java and interface with the delphi shared object?
Alternatively, I am looking for a way to do push notifications in a Delphi Xe5 android app.
I am glad to see delphi is evolving and adapting to current needs. The post made me curios so I looked around a little bit so I came across these resources:
forum post on embarcadero that recommends using datasnap to solve the communication issue between GCM and the delphi side.
I hope this will help somebody.
I got GCM working with Delphi and I made a sample component to take care of registering and receiving the GCM messages.
NOTE: This is just a rough test code, I'm not using it in any real application (yet). Please fell free to modify and improve, and if you find bugs, please post back.
Big thanks to Brian Long and his article on Android Services.
Get your GCM sender ID (it's your Project Number from gcm console) and your GCM API id (create a key for server application in GCM console), you'll need them (see the pictures at the bottom).
First of all, you need a modified classes.dex file. You can create this by running the bat file Java dir in the archive, or you can use the one that is already compiled by me (also included in the archive).
You have to ADD the new classes.dex to your Android Deployment and UNCHECK the embarcadero one:
Then, you need to edit your AndroidManifest.template.xml and add right after
<%uses-permission%>
:and right after
android:theme="%theme%">
In your application, declare the gcmnotification unit:
and then in your form declare a variable of the TGCMNotification type and a procedure that you will link to the TGCMNotification.OnReceiveGCMNotification event:
Put in the SenderID your GCM Project Number. To register you APP with GCM, call DoRegister:
If the DoRegister returns true (successfully registered), gcmn.RegistrationID will have the unique ID you need for sending messages to this device.
And you'll receive messages in event procedure:
.. and that's ALL you need it for Receiving. Cool, huh? :-)
To send, just use TIdHttp:
Next I'm going to post the units you need, just save them in the same place with your application (or just download the whole thing from HERE).
gcmnotification.pas
uGCMReceiver.pas
Here is the modified AndroidManifest.template.xml
And the full source for the test application (it will send and receive a GCM message):
The GCMReceiver.java that you need to compile and add it to the classes.dex is:
And HERE is the zip archive with the source.
If you have trouble making it work, it's probably something not configured right in you GCM console.
Here is what you need from you GCM console:
Project number (you use this when you register with GCM, place it into TGCMNotification.SenderID before calling DoRegister).
API ID you'll use this to send messages to the registered devices.
I think that creating a bridge to Java could be a good idea. Take a look at this post about how to do it. And here you can find a tutorial for implementing client side GCM in Java.
You interface Java with Delphi using JNI. The JNI allows you to call in both directions: Java to Delphi or Delphi to Java. So you can extend your Delphi applications with Java classes.
For implementing what you want on Android, doing it in Java will be the easier path to follow, as some available examples do exactly what you have in mind. Just have a look:
To interface Java JNI and Delphi, you can follow detailed steps, thus allowing a smooth communication between front end and back end of your application.
If you decide to re-use some of the code, remember to give appropriate credit to the authors.