fcm push notification example / send post request to fcm api java sample / fcm api post È£Ãâ
push notification Àü¼ÛÀ» À§ÇØ firebase cloud console api¸¦ È£Ãâ ÇÏ´Â ¹æ¹ý Áß
ÀÚ¹Ù ¼¹ö¿¡¼ È£Ãâ ÇÏ´Â ¹æ¹ýÀÔ´Ï´Ù.
1. function sendPost() ÀÛ¼º
public String sendPost(String url, String parameters) throws Exception {
TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager(){
public X509Certificate[] getAcceptedIssuers(){return new X509Certificate[0];}
public void checkClientTrusted(X509Certificate[] certs, String authType){}
public void checkServerTrusted(X509Certificate[] certs, String authType){}
}};
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, trustAllCerts, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
//reuqest header
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Authorization", "key=Firebase API KEY");
String urlParameters = parameters;
//post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.write(urlParameters.getBytes("UTF-8"));
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
System.out.println("Post parameters : " + urlParameters);
System.out.println("Response Code : " + responseCode);
StringBuffer response = new StringBuffer();
if(responseCode == 200){
BufferedReader in = new BufferedReader( new InputStreamReader(con.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
}
//result
System.out.println(response.toString());
return response.toString();
}
2. »ç¿ë¹æ¹ý
String url = "https://fcm.googleapis.com/fcm/send";
String parameters = "{"+
"\"data\": {"+
"\"message\": {" +
"\"title\": \"test\","+
"\"content\": \"test content\","+
"\"imgUrl\": \"\","+
"\"link\": \"\","+
"}"+
"},"+
"\"to\": "\"/topics/noticeMsg\""+
"}";
String result = sendPost(url,parameters);
-> "{\"data\" : {\"message\" : {\"title\":\"test\",\"content\":\"test content\",\"imgUrl\":\"\",\"link\":\"\"}},\"to\" : \"/topics/noticeMsg\"}";
(¿ÀŸ°¡ ÀÖÀ» ¼ö ÀÖ½À´Ï´Ù)
¿ÀŸ³ª ¾ÈµÇ´Â ºÎºÐ ÀÖÀ¸¸é ´ñ±Û ´Þ¾Æ ÁÖ¼¼¿ä