Google App Engine (GAE), Urban Airship, Java, Push Notifications. Sample code?

My server runs on GAE (Java), and I use the Urban Airship service to deliver push notifications. Of course, everything works fine when I use my web interface to send a test notification, but I would like to add a test button to my GAE application / server so that it starts UA to send push.

The problem is that all the examples I've seen so far are not compiled using the Java GAE libraries.

Does anyone have a sample Java code that they would like to share with this assembly and is running GAE to trigger a push notification through Urban Airship?

Thank!

+3
source share
4 answers

Here is a sample Java code that runs 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!

+6
source

Just in case, someone is trying to use the Google mechanism to send push notifications via urbanairhip to iOS devices, this is what finally helped me! This is for API V3.

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);
        }
+2
source

, Urban Airship , java-apns-gae.

Java APNS , ( ) Google App Engine.

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

+1
source

urbanairship4j (available on Google Code) uses Google HTTP Java. The client works great on AppEngine, Android, etc.

0
source

Source: https://habr.com/ru/post/1764255/


All Articles