我正在使用适用于 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 将其发送到服务。?
谢谢