Java Server – 使用POST发送Push到Google Firebase Cloud
在我测试推送通知正在与Postman合作时,我想在我的应用程序中发送消息时向FCM发送推送请求。 调用的函数将转到我的Java服务器并调用如下函数:
@POST @Consumes(MediaType.APPLICATION_JSON) public Response sendMsg(Message m) throws ExceptionFacade { ... }
因此,每次调用此函数时,我都希望使用json向https://fcm.googleapis.com/fcm/send发送POST请求。
我想知道是否已经为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); }