2014-10-07 157 views
0

我有一個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> 

}

一些想法?

回答

2

可能是你已經解決了這個問題。但它可以幫助別人,所以我發佈了答案。

選擇任何其他客戶端(澤西休息客戶端)或正常的http URL發佈。

發佈請求https://android.googleapis.com/gcm/send URL

設置兩個變量來HTTP頭

授權:鍵= YOUR_API_KEY 內容類型:應用/ JSON

有效載荷JSON應如下所示。

{ 
     "registration_ids" : ["APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx..."], 
     "data" : { 
     ///your data goes here 
     }, 
    } 

例如

{ "data": { 
     "score": "5x1", 
     "time": "15:10" 
     }, 
     "registration_ids": ["4", "8", "15", "16", "23", "42"] 
    } 

音符 - registration_ids是設備註冊ID。

現在發佈到其餘。

欲瞭解更多信息,請檢查this

示例代碼

包com.ajs.ajstest.gcmpost;

import java.io.IOException; 
import java.io.UnsupportedEncodingException; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.entity.StringEntity; 
import org.apache.http.impl.client.DefaultHttpClient; 

/** * * @author尼爾默爾 */ 公共類GCMPost {

public static void main(String ar[]) throws UnsupportedEncodingException, IOException{ 
HttpClient httpclient = new DefaultHttpClient(); 

    String json="{ \"data\": {" + 
       " \"score\": \"5x1\"," + 
       " \"time\": \"15:10\"" + 
       " }," + 
       " \"registration_ids\": [\"4\", \"8\", \"15\", \"16\", \"23\", \"42\"]" + 
       "}"; 
    HttpPost httpost = new HttpPost("https://android.googleapis.com/gcm/send"); 
httpost.setHeader("Content-Type", "application/json"); 
httpost.setHeader("Authorization","YOUR_API_KEY"); 

httpost.setEntity(new StringEntity(json)); 
HttpResponse response = httpclient.execute(httpost); 
} 

}

行家依賴關係

<dependency> 
    <groupId>org.apache.httpcomponents</groupId> 
    <artifactId>httpclient</artifactId> 
    <version>4.3.6</version> 
    <type>jar</type> 
</dependency> 
相關問題