推送通知模块发送空

时间:2016-02-22 08:08:31

标签: java android push-notification google-cloud-messaging

我正在尝试使用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());

1 个答案:

答案 0 :(得分:0)

您使用的网址不正确。它应该是https://gcm-http.googleapis.com/gcm/send。请参阅Server changes

相关问题