자바 스크립트 코드를 통해 gcm 메시지를 보내려고합니다. 이를 위해 json 객체를 게시해야합니다.gcm 메시지 (자바 스크립트)
url 및 json 객체 형식은 gcm 문서()로 제공됩니다.
테스트 목적으로 완벽하게 작동하는 Java 코드를 작성했습니다. 하지만 자바 스크립트 코드가 작동하지 않습니다. 누구든지 샘플 코드 (javascript for gcm)가 있으면 게시하십시오.
String body = "registration_id=proper_id&data.number=12345678";
byte[] bytes = body.getBytes();
HttpURLConnection conn = getConnection(url);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setFixedLengthStreamingMode(bytes.length);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
conn.setRequestProperty("Authorization", "key=" + key);
OutputStream out = conn.getOutputStream();
out.write(bytes);
자바 스크립트 코드 :
var http = new XMLHttpRequest();
var url = "https://android.googleapis.com/gcm/send";
http.onreadystatechange = function() {
if (http.readyState == 4 && http.status == 200) { document.getElementById("target").innerHTML = http.responseText;
}
}
http.open("POST", url, false);
http.setRequestHeader("Content-type", "application/json");
http.setRequestHeader("Authorization", "key=proper_api_key");
var data = '{ "collapse_key": "qcall","time_to_live": 108, "delay_while_idle": true,"data": {"number":"12345678"},"registration_ids":["proper_id"]}';
http.send(data);