views:

128

answers:

1

My server is running on GAE (Java), and I'm using Urban Airship service to deliver push notifications. Of course, everything works fine when I use their web-interface to send a test notification, but I'd like to add a test-button to my GAE app/server to have it trigger UA to send the push.

The problem is, all of the examples I've seen so far don't compile against GAE's Java libraries.

Does anyone have any java sample code they'd like to share that build & runs under GAE to trigger a push notification through Urban Airship?

Thanks!

+1  A: 

Here is some Java sample code that works under GAE and sends a push notification through Urban Airship:

URL url = new URL("https://go.urbanairship.com/api/push/");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);

String appKey = "YOUR APP KEY HERE";
String appMasterSecret = "YOUR MASTER SECRET HERE";

String authString = appKey + ":" + appMasterSecret;
String authStringBase64 = Base64.encodeBase64String(authString.getBytes());
authStringBase64 = authStringBase64.trim();

connection.setRequestProperty("Content-type", "application/json");
connection.setRequestProperty("Authorization", "Basic " + authStringBase64);

String jsonBodyString = "YOUR URBAN AIRSHIP JSON HERE";

OutputStreamWriter osw = new OutputStreamWriter(connection.getOutputStream());
osw.write(jsonBodyString);
osw.close();

int responseCode = connection.getResponseCode();
// Add your code to check the response code here

Hope this helps!

loomer
I had figured this out and forgotten to come back and post but, yes, your example is pretty much what I needed. The part I was missing was the setRequestProperty Authorization line -- I had somehow skimmed over that and, of course, it made everything not-work. Thanks!
Olie