2

I have a java server running with the spring framework. For now I have to develop a GCM service to send data from the server to an Android app.

I have been seeing some tutorial on Internet and all of them implements this service in a php script. I'm wondering if I can use them in my java server with spring.

I know I can rewrite the code to Java but I don't have so much time as it's a work for University, so I don't wanna waste much time rewriting all of this..

So I'd like to know if it is possible to add these php scripts and if so, how can I do it?

Eran
  • 387,369
  • 54
  • 702
  • 768
Felipe Mosso
  • 3,907
  • 11
  • 38
  • 61

3 Answers3

3

Even if you interface your Java server with some PHP code that sends GCM messages, you would still have to pass registration IDs and notification payload to that code. Wouldn't it be easier to simply use the gcm-server.jar supplied by Google?

Using gcm-server.jar, the code you need to send a GCM message is as simple as this :

Sender sender = new Sender(apiKey);
Message message = new Message.Builder()
    .delayWhileIdle(true)
    .addData("key1", "value1")
    .addData("key2", "value2")
    .build();
Result result = sender.send(message, registrationId, numOfRetries);

If you wish to handle error responses, you should check the result instance to see if the message was successfully received by the GCM server, and if not, what error occurred.

The only other thing you'll have to implement is some servlet that accepts registration IDs from your Android app and stores them in your DB. Perhaps you can use existing PHP code for that part (you won't need any interface between the PHP code and Java code).

Eran
  • 387,369
  • 54
  • 702
  • 768
  • Oooww! That was what I looking for! I will test this piece of code it seems to be very easy! What about sending the ID from Android app to server, is there any trick that gsm library does, or would I have to send it by using Rest service or something like that? – Felipe Mosso Jul 03 '13 at 20:11
  • @FelipeMosso For sending the ID you'll need some servlet or JSP or something like that. The Google library doesn't supply that, since it makes no assumptions regarding what you intend to send from your app to your server (you may decide to send additional info beside the registration ID), and it doesn't make any assumptions regarding how you intend to store the data in your server. – Eran Jul 03 '13 at 20:16
  • Alright!! Thank you so much! I'm going to try it, I have also got some samples from android sdk! Thanks very much for giving me the direction! – Felipe Mosso Jul 03 '13 at 20:26
0

you can execute external programs from your java code

you will have to pass the data back and forth somehow. reading/writing stdin and stdout probably

maby this will help: Execute external program in java

Community
  • 1
  • 1
The Surrican
  • 29,118
  • 24
  • 122
  • 168
0

Yes, you can. They have interpreters for PHP, JavaScript, and even Python written in pure Java. For PHP, one such offering is Caucho Quercus.

CodeChimp
  • 8,016
  • 5
  • 41
  • 79
  • I have no idea what you just said. OP asked if you can run PHP in a Java/Spring web application, which I believe you can with the included link, which is a WAR deployment of a PHP interpreter written in Java. – CodeChimp Jul 03 '13 at 20:04
  • that was no critisism, only a pointed remark regarding java ;) – The Surrican Jul 03 '13 at 21:42