Reputation: 24675
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!
Upvotes: 3
Views: 3507
Reputation: 13620
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
Upvotes: 1
Reputation: 994
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);
}
Upvotes: 2
Reputation: 2017
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!
Upvotes: 6
Reputation: 43
urbanairship4j (available on Google Code) uses Google HTTP Java Client so works perfectly on AppEngine, Android, etc.
Upvotes: 0