在我测试了推送通知与 Postman 的配合后,我想在我的应用程序中发送消息时向 FCM 发送推送请求。调用的函数将访问我的 Java 服务器并调用如下函数:
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response sendMsg(Message m) throws ExceptionFacade {
...
}
所以每次我调用这个函数时我想发送一个 POST 请求到https://fcm.googleapis.com/fcm/send https://fcm.googleapis.com/fcm/send与 json.
我想知道是否已经有用于 java 服务器的代码?以及如何实施它的一些帮助。
另外我不明白我是否可以使用 php 文件来做到这一点(我发现这样的事情https://github.com/Paragraph1/php-fcm https://github.com/Paragraph1/php-fcm)。我正在使用 AngularJS。
感谢你们 !
这是运行良好的最终代码!
它发送一个像这样的 json :
{
"to" : "...",
"priority" : "high",
"notification" : {
"title" : "hello",
"body" : "me"
}
}
//不要忘记添加 common-codec 和 common-login jar 以确保构建成功。
public class JavaApplication1 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws JSONException, IOException {
HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost("https://fcm.googleapis.com/fcm/send");
post.setHeader("Content-type", "application/json");
post.setHeader("Authorization", "key=FCM-API-KEY");
JSONObject message = new JSONObject();
message.put("to", "TOKEN-FCM-OF-THE-DEVICE");
message.put("priority", "high");
JSONObject notification = new JSONObject();
notification.put("title", "Me");
notification.put("body", "New message");
message.put("notification", notification);
post.setEntity(new StringEntity(message.toString(), "UTF-8"));
HttpResponse response = client.execute(post);
System.out.println(response);
System.out.println(message);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)