在我测试了推送通知是否可以与 Postman 配合使用后,我想在我的应用程序中发送消息时向 FCM 发送推送请求。调用的函数将访问我的 Java 服务器并调用如下函数:
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response sendMsg(Message m) throws ExceptionFacade {
...
}
所以每次我调用这个函数时,我都会向https://fcm.googleapis.com/fcm/send发送一个POST请求。带有 json。
我想知道是否已经有适用于 java 服务器的代码?还有一些如何实现它的帮助。
另外,我不明白是否可以使用 php 文件来完成此操作(我发现类似 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);
}
关于Java 服务器 -- 使用 POST 发送 Push 到 google Firebase Cloud,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39159704/