3

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!

Olie
  • 24,597
  • 18
  • 99
  • 131
  • To clarify: Urban Airship isn't a requirement. My goal is for my GAE(java) app to trigger APNS. I added UA a few days ago, thinking it would help. However, if your solution gets me direct-from-GAE, or through some other free, reliable service (UA is free-to-start, like GAE), I'm ok with that. – Olie Sep 11 '10 at 01:21

4 Answers4

6

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(), "UTF-8");
osw.write(jsonBodyString);
osw.close();

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

Hope this helps!

loomer
  • 2,017
  • 1
  • 17
  • 11
  • 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 Oct 24 '10 at 14:52
  • The answer doesn't work for special characters like german umlauts (öäü) under GAE. It works perfectly in the text-environment on my mac with delivery to the iPhone. So I guess it is an encoding problem I just cannot get solved. Any ideas? – Marcus Franzen Dec 08 '11 at 19:55
  • I've edited the example code to show how to set the encoding on the OutputStreamWriter. – loomer Oct 23 '12 at 08:11
2

Just in case anyone is trying to use Google app engine to send push notification via urbanairship, to iOS devices, this is what finally worked for me! This is for V3 API.

import org.apache.commons.codec.binary.Base64; //commons-codec-1.6.jar

try {
            URL url = new URL(URBAN_AIRSHIP_PUSH_URL);
            String nameAndPassword = DEV_API_KEY+":"+DEV_API_MASTER_SECRET;

            String authorizationHeader = Base64.encodeBase64String(nameAndPassword.getBytes("UTF-8"));
            authorizationHeader = "Basic "+authorizationHeader;

            HTTPRequest request = new HTTPRequest(url, HTTPMethod.POST);
            request.addHeader(new HTTPHeader("Authorization", authorizationHeader));
            request.addHeader(new HTTPHeader("Content-type", "application/json"));
            request.addHeader(new HTTPHeader("Accept", "application/vnd.urbanairship+json; version=3;"));

            logger.info("Authorization header for push:"+authorizationHeader);
            logger.info("PushMessage payload:"+notificationPayload);
            request.setPayload(notificationPayload.getBytes("UTF-8"));

            URLFetchService urlFetchService = URLFetchServiceFactory.getURLFetchService();
            HTTPResponse fetchedResponse = urlFetchService.fetch(request);

            if (fetchedResponse.getResponseCode() >= 400) {
                logger.warning("Push notification failed:"+new String(fetchedResponse.getContent(), "UTF-8")+
                    "response code:"+fetchedResponse.getResponseCode());
            } else {
                logger.info("PushMessage send success");
            }
        } catch (MalformedURLException e) {
            logger.log(Level.SEVERE, "PushMessage failed", e);
        } catch (IOException e) {
            logger.log(Level.SEVERE, "PushMessage failed", e);
        }
Puran
  • 984
  • 6
  • 15
1

Because you said Urban Airship isn't a requirement let me recommend java-apns-gae.

It's an open-source Java APNS library that was specifically designed to work (and be used) on Google App Engine.

https://github.com/ZsoltSafrany/java-apns-gae

Zsolt Safrany
  • 13,290
  • 6
  • 50
  • 62
  • Do you have an example project that uses your Git project on app-engine? I am talking about something similar to the example project https://github.com/GoogleCloudPlatform/solutions-ios-push-notification-sample-backend-java. For whatever reason, the google project is not working for me. So if you don't mind sharing a simple project that works, it would be great. Thanks. – Katedral Pillon Nov 10 '14 at 21:02
  • @KatedralPillon I cannot share an example project but you may check out the docs of `java-apns-gae` - it contains some example code. http://zsoltsafrany.github.io/java-apns-gae – Zsolt Safrany Nov 11 '14 at 13:41
  • I have already been looking at the docs. No luck. The Google project I mentioned seems similar enough to yours that you could modify it. If ever you can make some time to help some complete strangers, it would help a lot of people. Thanks. – Katedral Pillon Nov 11 '14 at 13:44
0

urbanairship4j (available on Google Code) uses Google HTTP Java Client so works perfectly on AppEngine, Android, etc.

dleshem
  • 43
  • 5