我有一個java restful webservice(1)和一個Android應用程序,它使用了這個(2)。第三個應用程序負責通過web服務將數據輸入到數據庫中。我希望當第三個應用程序在我的數據庫中輸入新數據時,webservice會通過GCM通知android應用程序。我的Android應用程序能夠接收reg-Id GCM。 (我有在谷歌開發等創建的項目,服務器API密鑰..)如何發送消息從java restful服務器到gcm服務器
我試過這個:當第三個應用程序訪問URI輸入數據通過POST,發送消息到谷歌服務器。不工作
@POST
@Override
@Consumes({"application/xml", "application/json"})
public void create(Usuarios entity) {
String deviceId=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX...;
Sender sender = new Sender("AIzaSyXXXXXXXXXXXXXXXXXXXXXXXXXXX");
Message message = new Message.Builder()
.addData("message", "this is the message")
.addData("other-parameter", "some value")
.build();
Result result = sender.send(message, deviceId, 3);
super.create(entity);
}
我也試圖創建一個servlet。但不工作
@WebServlet(name = "NewServlet_1", urlPatterns = {"/NewServlet_1"})
public class NewServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet NewServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet NewServlet at " + request.getContextPath() + "</h1>");
out.println("</body>");
out.println("</html>");
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Sender sender = new Sender("AIzaSyBNFur3sFJpMujkVYW9rzEq3udI0khRCOI");
Message message = new Message.Builder()
.addData("message", "this is the message")
.addData("other-parameter", "some value")
.build();
Result result = sender.send(message, "xxxxxxxxxxxx", 3);
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
一些想法?