开发者

JSON payload URL Issue android

开发者 https://www.devze.com 2023-04-12 17:47 出处:网络
Am using Urban airship push notifications for Android. In that , i want to use broadcast to send notifications all my users. While using that, am getting 400 bad request error.

Am using Urban airship push notifications for Android. In that , i want to use broadcast to send notifications all my users. While using that, am getting 400 bad request error.

Please tell me whats wrong in my code:

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

           URL url = new URL("https://go.urbanairship.com/api/airmail/s开发者_JAVA技巧end/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);

Actual JSON Payload that i want to send is:

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

or also if you have sample code for using urban airpship push notifications broadcast api please share here .

How to send this payload to service by using HttpsURLConnection.?

Thanks


This is how you "build" your 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);


the JSON you create in your code only contains title and message. Lacks

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

and

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

doesn't it?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号