1

I have a Java web server which I want to send UA push notifications through. I know this is possible through UA's dashboard, but I want to communicate with UA using the web-server directly.

I have already read this documentation, and have reviewed this SO question, but am still at a bit of a loss for where to start.

Can anyone please help point me toward a good starting point? Do I need to import any specific .jar files, etc?

I have been trying to adapt the following code snippet, but it doesn't seem to function properly. What am I doing wrong?

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());
Community
  • 1
  • 1

2 Answers2

0

You just need to send a POST request to the urbanairship server with your notification, including your AppId and Secret key encoded appropriately...

It looks like the link you included above is to a library which will do most of this for you.

Sample code, note: the PushNotificationObj in this example is a bean with the fields for the body of the push..

        URL url = new URL(URBAN_AIRSHIP_API_URL);
        HTTPRequest req = new HTTPRequest(url, HTTPMethod.POST);

        String authString = ApplicationProperties.getUrbanAirshipAppId() + ":" + ApplicationProperties.getUrbanAirshipMasterSecret();

        String authStringBase64 = new String(Base64.encodeBase64(authString.getBytes()));
        authStringBase64 = authStringBase64.trim();

        req.addHeader(new HTTPHeader("Content-Type", "application/json"));
        req.addHeader(new HTTPHeader("Authorization", "Basic " + authStringBase64));

        ObjectMapper mapper = new ObjectMapper();
        ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
        mapper.writeValue(byteStream, new PushNotificationObj(userEmail, message));

        req.setPayload(byteStream.toByteArray());

And then send the request...

Rick Mangi
  • 3,761
  • 1
  • 14
  • 17
  • do you mean a simple HTTPPost request to the URL https://go.urbanairship.com/api/push/" ?? – user1270598 Jul 12 '12 at 16:27
  • thanks..il try that..i noticed the body would be the json entities as described in the documentation..how do i include the appid and secret key? Sorry am new to this and UA doesn't provide java documentation for connection to their servers...so kinda confused. – user1270598 Jul 12 '12 at 19:00
0

You might want to try urbanairship4j (available on Google Code). It uses Google HTTP Java Client so works perfectly on any Java environment (inc. AppEngine, Android, etc).

dleshem
  • 43
  • 5