Tuesday 16 December 2014

GCM Integration (Java Server)


Hello Friends,

I will code here GCM AppServer in java.

You need gcm-server.jar and json-simple-1.1.1 jar
click here to get jar files.



Don't forget to import this two jar....


Here two servlet class one for getting registration id from android app.
Other for sending message to GCM server which will in turn sent to android app.
One jsp file name index.jsp the message we pass on this will forwarded to android app through GCM.

Following is RegisterServlet class....

package in.blogspot.longjamcode;

import java.io.BufferedWriter;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**

 * Servlet implementation class RegisterServlet
 */
@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**

* @see HttpServlet#HttpServlet()
*/
public RegisterServlet() {
super();
// TODO Auto-generated constructor stub
}

/**

* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}

/**

* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// gcm registration id from client android device will get here

String gcm_id = request.getParameter("gcm_id");

  //we store gcm_id on a file which will be used latter

File file = new File("/YOUR_FILE_NAME.txt");
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(gcm_id);
bw.close();
}
}

Following is SentServlet Class this class will sent message to mobile device through GCM


package in.blogspot.longjamcode;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.android.gcm.server.Message;
import com.google.android.gcm.server.Sender;
import com.google.android.gcm.server.Result;

/**
 * Servlet implementation class SentServlet
 */
@WebServlet("/SentServlet")
public class SentServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final String GOOGLE_SERVER_KEY = "YOUR SERVER KEY";
static final String MESSAGE_KEY = "message";

/**
* @see HttpServlet#HttpServlet()
*/
public SentServlet() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// reading regId which save in file before
BufferedReader br = new BufferedReader(new FileReader("YOUR_FILE_NAME.txt"));
String regId = br.readLine();
br.close();

                //userMessage will get from jsp file
String userMessage = request.getParameter("message");
Sender sender = new Sender(GOOGLE_SERVER_KEY);
Message message = new Message.Builder().timeToLive(30)
.delayWhileIdle(true).addData(MESSAGE_KEY, userMessage).build();
System.out.println("regId: " + regId);
Result result = sender.send(message, regId, 1);
request.setAttribute("pushStatus", result.toString());
}
}

and following is index.jsp file....

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>App Server</title>
</head>
<body>

<h1>GCM Server in Java</h1>

<form action="SentServlet" method="post">

<div>

<--Message which is going to forward will get from this textarea-->
<textarea rows="2" name="message" cols="23"
></textarea>
</div>
<div>

<--When hit this will call the SentServlet class -->
<input type="submit" value="Push Notification" />
</div>
</form>
</body>
</html>


You can download full project...

full source code click here

No comments:

Post a Comment