我正在尝试使用gcm向已注册的设备发送通知。但它发送“null”而不是消息。我尝试了很多解决方案。如果缺少任何基本的东西,请分享。我附上了代码spinet。
JSONObject jGcmData = new JSONObject();
JSONObject jData = new JSONObject();
String msg = "new order came";
jData.put("message", msg);
// Where to send GCM message.
if (!(id == null)) {
jGcmData.put("to", id);
} else {
jGcmData.put("to", "/topics/global");
}
// What to send in GCM message.
jGcmData.put("data", jData);
// Create connection to send GCM Message request.
URL url = new URL("https://android.googleapis.com/gcm/send");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Authorization", "key=" + API_KEY);
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestMethod("POST");
conn.setDoOutput(true);
// Send GCM message content.
OutputStream outputStream = conn.getOutputStream();
outputStream.write(jGcmData.toString().getBytes());