无法推出自己的,我决定使用Parse发送推送通知。我一直在阅读他们的教程。我不清楚如何从App-Engine向特定用户发送推送。我正在处理以下场景。 给定用户有五百个朋友。当所述用户更新她的个人资料图片时,五百个朋友应该收到通知。我该怎么做这么简单的事情?这是非常简单的事情。那么我如何使用Parse做到这一点?我的服务器是Java App-engine。我需要知道app-engine部分是怎么做的。 (除了:我已经成功实现了app-engine到android推送。)
在某些情况下,这是我在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");
}
}
所以真正的问题是,Parse版本是什么样的?
我没有使用Urban Airship,因为他们想要每月200美元作为起始费用:这是零推送通知。然后,当我发送更多推动时,这笔钱应该会增加。 (他们只是改变了他们的定价模式)。所以我需要另一种选择;解析似乎有很多优惠。我只是不知道如何完成我需要的东西。
答案 0 :(得分:5)
Parse公开了一个RESTful API,您可以使用与您的示例类似的方式(稍微调整)。
当使用解析推送通知时,您希望发送内容的每个用户都由向Parse注册的“安装”对象表示。你可以在这里找到更多信息。可以通过安装REST API在安装上执行CRUD操作。
他们有2种推送方式:频道和'高级目标'。您应该可以使用“高级目标”来指定deviceTokens(就像您在示例中所做的那样)。
创建用户安装:
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.
}
正如您所料,可以通过向https://api/parse.com/1/installation/{objectId}
以同样的方式完成发送。只需用push api替换uri,用
替换jsonURL 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");
}
希望这有帮助
编辑:修复示例拼写错误,现在使用java.net类