Reputation: 14854
Unable to roll-out my own, I am deciding to use Parse to send push notifications. I have been reading their tutorials. It is not clear to me how I might send pushes from App-Engine to specific users. I am dealing with the following scenario. A given user has five hundred friends. When said user updates her profile picture, the five hundred friends should receive a notification. How do I do something so simple? This is very simple stuff. So how do I do that with Parse? My server is Java App-engine. I need to know how do to the app-engine part. (aside: I have already successfully implemented app-engine to android push).
For some context, here is what I had on app-engine for Urban Airship.
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 the question really is, what does the Parse version look like?
I am not using Urban Airship because they want to charge me $200/month as starting fee: that's for zero push notifications. And then that money is supposed to increase as I send more pushes. (They just changed their pricing model). So I need an alternative; parse seems to have a good deal. I just don't know how to accomplish what I need yet.
Upvotes: 2
Views: 1983
Reputation: 163
Parse exposes a RESTful API that you can use in a similar way to your example (with minor tweaks).
When using parse for push notifications, each user that you would want to send something to is represented by an "Installation" object registered with Parse. You can find more info on here. CRUD operations can be performed on Installations through their installation REST API.
They have 2 ways to send pushes: Channels and 'Advanced Targetting'. You should be able to use the 'Advanced Targetting' to specify the deviceTokens (as you did in your example).
Create User 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 accomplished in much the same way. Just replace the uri with the push api and the 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 this helps
EDIT: Fixed example typos and now using java.net classes
Upvotes: 5