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}发送PUT请求来更新此条目
发送方式大致相同.只需用push api和json替换uri即可
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");
}
希望这可以帮助
编辑:修复示例拼写错误,现在使用java.net类