gpt4 book ai didi

java - 如何从java Restful服务器向gcm服务器发送消息

转载 作者:行者123 更新时间:2023-12-02 05:24:03 25 4
gpt4 key购买 nike

我有一个 java Restful Web 服务 (1) 和一个使用该服务的 Android 应用程序 (2)。第三个应用程序负责通过 Web 服务将数据输入数据库。我希望当第三个应用程序在我的数据库中输入新数据时,Web 服务通过 GCM 通知 Android 应用程序。我的 Android 应用程序能够接收 reg-Id GCM。 (我在 google 开发人员等中创建了项目,服务器 api key ..)

我尝试了这个:当第三个应用程序访问 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>

}

一些想法??

最佳答案

也许你已经解决了问题。但这对其他人很有帮助,所以我发布了答案。

选择任何休息客户端(jersey休息客户端)或正常的http URL帖子。

将请求发送至https://android.googleapis.com/gcm/send网址

将两个变量设置为http header

授权:key=YOUR_API_KEY内容类型:application/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;

/** * * @作者尼尔马尔 */公开课 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);
}

}

Maven 依赖

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.6</version>
<type>jar</type>
</dependency>

关于java - 如何从java Restful服务器向gcm服务器发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26234338/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com