Send push notification using Parse from engine app

Unable to deploy my own, I decided to use Parse to send push notifications. I am reading their tutorials. It is not clear to me how I can send push applications from App-Engine to specific users. I am dealing with the following scenario. This user has five hundred friends. When a user updates their profile picture, five hundred friends should receive a notification. How can I do something so simple? This is a very simple material. So how do I go about doing this with Parse? My server is a Java application. I need to know how to do this with a part of the application. (aside: I've already successfully implemented an android-android engine app).

In some context, this is what I had in the Urban Airship app.

try {
            URL url = new URL("https://go.urbanairship.com/api/push/");
            String appKey = "my app key";
            String appMasterSecret = "my master key";
            String nameAndPassword = appKey + ":" + appMasterSecret;

            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;"));

            log.info("Authorization header for push:" + authorizationHeader);
            String jsonBodyString = String.format(JSON_FORMAT, deviceTokens.toString(), alert);
            log.info("PushMessage payload:" + jsonBodyString);
            request.setPayload(jsonBodyString.getBytes("UTF-8"));

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

            if (fetchedResponse.getResponseCode() >= 400) {
                log.warning("Push notification failed:" + new String(fetchedResponse.getContent(), "UTF-8") +
                    "response code:" + fetchedResponse.getResponseCode());
            } else {
                log.info("PushMessage send success");
            }
        }

      

So really the question is, what does the Parse version look like?

I don't use Urban Airship because they want to charge me $ 200 per month as a start-up fee: that's for zero push notifications. And then this money should increase as I send more shocks. (They just changed their pricing model). So I need an alternative; The syntax seems to have a lot. I just don't know how to accomplish what I need.

+3


source to share


1 answer


Parse provides a RESTful API that can be used similar to your example (with minor changes).

When using parsing for push notifications, each user to whom you want to send something is represented by a Setup object registered with Parse. You can find more information here. CRUD operations can be performed on installations through their installation REST API.

They have 2 ways to send push: Channels and 'Advanced Targetting'. You should be able to use Extended Job to specify deviceTokens (as in your example).

Create custom installation:

URL target = new URL("https://api.parse.com/1/installation");
HttpURLConnection connection = (HttpURLConnection) target.openConnection();
connection.setRequestMethod("PUT");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("X-Parse-REST-API-KEY", "${REST_API_KEY}");
connection.setRequestProperty("X-Parse-Application-Id", "${APPLICATION_ID}");

connection.setDoInput(true);
connection.setDoOutput(true);

String installationCreation = "{\"appName\":\"Your App Name\"," +
        "\"deviceType\":\"android\",\"deviceToken\":\"" + userDeviceToken + "\"}";
try (OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream())) {
    out.write(installationCreation);
}

connection.connect();
if (connection.getResponseCode() != 201) {
    log.error("User Create Failed");
} else {
    String response = connection.getResponseMessage();
    // response content contains json object with an attribute "objectId" 
    // which holds the unique user id. You can either use this value or 
    // deviceToken to send a notification.
}

      



As you would expect, this entry can be updated by sending a PUT request to https://api/parse.com/1/installation/{objectId}

Sending is done in much the same way. Just replace uri with push api and json with

URL target = new URL("https://api.parse.com/1/push");
HttpURLConnection connection = (HttpURLConnection) target.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("X-Parse-REST-API-KEY", "${REST_API_KEY}");
connection.setRequestProperty("X-Parse-Application-Id", "${APPLICATION_ID}");

connection.setDoInput(true);
connection.setDoOutput(true);

String notification = 
        "\"where\": {\"deviceType\": \"android\",\"deviceToken\": { \"$in\" :" + deviceTokens.toString() + "}},"+
        "\"data\": {\"alert\": \"A test notification from Parse!\" }";
try (OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream())) {
    out.write(notification);
}

connection.connect();
if (connection.getResponseCode() != 201) {
    log.error("Notification Failed");
}

      

Hope it helps

EDIT: Fixed example typos and now using java.net classes

+5


source







All Articles