0

Android用のUrbanairshipプッシュ通知を使用しています。その中で、ブロードキャストを使用してすべてのユーザーに通知を送信したいと思います。それを使用している間、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こちらで共有してください。

payloadHttpsURLConnectionを使用してこれをサービスに送信する方法。

ありがとう

4

2 に答える 2

1

これは、JSON ペイロードを「構築」する方法です。

        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 に答える