Java Server — send Push with POST to google Firebase Cloud

帅比萌擦擦* 提交于 2019-12-02 09:47:52

Here is the final code working well ! It is sending a json like this :

{
 "to" : "...",
 "priority" : "high",
 "notification" : {
                   "title" : "hello",
                   "body" : "me"
  }
}

//Don't forget to add common-codec and common-login jar for build success.

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);
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!