我有一个 Java Web 服务器,我想通过它发送 UA 推送通知。我知道这可以通过 UA 的仪表板实现,但我想直接使用 Web 服务器与 UA 通信。
我已经阅读了这个文档,并且已经查看了这个 SO question,但是我仍然不知道从哪里开始。
谁能帮我指出一个好的起点?我需要导入任何特定的 .jar 文件等吗?
我一直在尝试调整以下代码片段,但它似乎无法正常运行。我究竟做错了什么?
URL mURL=new URL("https://go.urbanairship.com/api/push/broadcast");
HttpsURLConnection connection = (HttpsURLConnection)mURL.openConnection();
connection.setRequestMethod("POST");
JSONObject mPayload=new JSONObject();
JSONObject mPayload1=new JSONObject();
JSONObject mPayload2=new JSONObject();
mPayload1.put("extra", mPayload2);
mPayload1.put("alert", "hello world");
mPayload2.put("phonenumber", "12345");
mPayload.put("android", mPayload1);
System.out.println(mPayload.toString());
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.addRequestProperty("Content-Type","application/json;charset=UTF-8");
String authString = "**MY APP KEY**" + ":" + "**MASTER KEY**";
String authStringBase64 = new String(Base64.encodeBytes(authString.getBytes()));
authStringBase64 = authStringBase64.trim();
connection.addRequestProperty("Authorization", "Basic " + authStringBase64);
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.write(mPayload.toString().getBytes());
wr.flush();
Log.d("print", "response code"+ connection.getResponseCode());