Posted on

Sending Apple iOS Notifications via Urban Airship on Google App Engine

In this post I’ll show you how to send an Apple Notification from Google App Engine via Urban Airship. I was mightily impressed how easy it was to do earlier this week. Their docs are excellent, and I love when apps use curl for their API examples – it means the API is simple, and the examples are perfectly transparent.

Bit of background (if you don’t care skip to the code), we’re working on an iOS app for OrderPipe which will allow users to set daily revenue or order goals and be notified when they hit them.

Notifications are configured through the mobile app like so:

The email version looks a bit like this currently:

So that’s via email which is not terribly exciting, the iOS notifications are coming very soon too, along with the companion iOS app for the goal setup. Now, on with the code.

The Java code

On Google App Engine I used the low-level UrlFetch API. Here’s what you need to make it work. I should add that there is already a library available for doing this, but I did not need all the extra features and just wanted a super-simple call to send a message without importing yet-another-library. So if you have the same requirements as me, here’s what you need to do.

First you need to build an actual request object like this:

String urlString = "https://"+URBAN_AIRSHIP_BASE_URL+"/api/push/";
// device token sent by the iOS app to our server via an API
String deviceToken = "ABCDEFG..."; 
// created server side when a notification is required.
String msg = "You sold something"; 
URL url = new URL(urlString);
String nameAndPassword = APP_KEY+":"+APP_MASTER_SECRET;
String authorizationString = "Basic " + Base64.encode(nameAndPassword.getBytes());
HTTPRequest request = new HTTPRequest(url, HTTPMethod.POST);
request.addHeader(new HTTPHeader("Authorization", authorizationString));
request.addHeader(new HTTPHeader("Content-Type", "application/json"));
String jsonEncodedMsg = new String(JsonStringEncoder.getInstance().quoteAsString(msg));
String jsonPayload = 
		"{\"device_tokens\": [\""+deviceToken+"\"], " +
		"\"aps\": {\"alert\": \""+jsonEncodedMsg+"\"}}";
return request;

I defined these constants, you can get them from your Urban Airship account section:

private static final String URBAN_AIRSHIP_BASE_URL = "";
public static String APP_KEY = "ABC12345";
public static String APP_MASTER_SECRET = "ABC12345";

Once you have your request you can send the message like this:

URLFetchService urlFetchService = URLFetchServiceFactory.getURLFetchService();
HTTPRequest serviceRequest = getSendRequest(deviceToken, msg); // go build the request as above
HTTPResponse fetchedResponse = urlFetchService.fetch(serviceRequest);
if (fetchedResponse.getResponseCode() != 200) {
    // something went wrong...

And that’s all there is to it. If you have to send many such notifications in your app, consider backgrounding them work by using the Async UrlFetch.

2 thoughts on “Sending Apple iOS Notifications via Urban Airship on Google App Engine

  1. If you need beta testers for the iOS app, I’d be willing to help.

  2. That’d be great Jon, I shall be in touch soon!

Comments are closed.