0

我正在使用适用于 Android 的 Urban 飞艇推送通知。在那,我想使用广播向我的所有用户发送通知。使用它时,我收到 400 错误请求错误。

请告诉我我的代码有什么问题:

Authenticator.setDefault(new Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                       return new PasswordAuthentication("sixxxxxxw","YSxxxxxxxxxx:Fxxxxxxxxxxxx".toCharArray());
                       }
                });

           URL url = new URL("https://go.urbanairship.com/api/airmail/send/broadcast/");
           HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();
           connection.setRequestMethod("POST");
           connection.setDoOutput(true);
           connection.setDoInput(true);
           connection.setUseCaches(false);


           connection.setRequestProperty("Content-Type","application/json");
           //connection.setRequestProperty("Content-Length", Integer.toString(data.length()));
           JSONObject json = new JSONObject();
           json.put("title","My title");
           json.put("message","My message");



           try {
               output = connection.getOutputStream();
               output.write(json.toString().getBytes());
             } finally {
               if (output != null) { output.close(); }
             }

             int status = ((HttpURLConnection) connection).getResponseCode();
             System.out.println("status is..." + status);

我要发送的实际JSON Payload是:

{
    "push": {
        "aps": {
            "alert": "New message!"
        }
    },
    "title": "Message title",
    "message": "Your full message here.",
    "extra": {
        "some_key": "some_value"
    }
}

或者如果你有sample code for using urban airpship push notifications broadcast api请在这里分享。

如何payload使用 HttpsURLConnection 将其发送到服务。?

谢谢

4

2 回答 2

1

这就是您“构建” JSON PayLoad 的方式:

        JSONObject json = new JSONObject();
        JSONObject push = new JSONObject();
        JSONObject aps = new JSONObject();
        JSONObject extra = new JSONObject();
        aps.put("alert", "New message!");
        push.put("aps", aps);
        json.put("push", push);
        json.put("title","My title");
        json.put("message","My message");
        extra.put("some_key","some_value");
        json.put("extra", extra);
于 2011-10-11T10:46:09.597 回答
0

您在代码中创建的 JSON 仅包含标题和消息。缺乏

"push": {
    "aps": {
        "alert": "New message!"
    }
},

"extra": {
    "some_key": "some_value"
}

不是吗?

于 2011-10-11T10:45:42.787 回答