由于无法推出自己的产品,我决定使用 Parse发送推送通知。我一直在阅读他们的教程。我不清楚如何从 App-Engine 向特定用户发送推送。我正在处理以下场景。 某个用户有五百个 friend 。当所述用户更新她的个人资料图片时,五百个 friend 应该收到通知。我该如何做这么简单的事情?这是非常简单的事情。那么我该如何使用 Parse 做到这一点呢?我的服务器是Java应用程序引擎。我需要知道如何处理应用程序引擎部分。 (旁白:我已经成功地将应用程序引擎实现到 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 美元/月的起始费:这是零推送通知的费用。然后,随着我发送更多的推送,这笔钱应该会增加。 (他们只是改变了定价模式)。所以我需要一个替代方案; parse 似乎有很多好处。我只是不知道如何完成我需要的东西。
最佳答案
Parse 公开了一个 RESTful API,您可以按照与示例类似的方式使用它(进行一些细微的调整)。
当使用解析推送通知时,您想要发送内容的每个用户都由在解析中注册的“安装”对象表示。您可以在这里找到更多信息。可以通过安装 REST API 在安装上执行 CRUD 操作。
他们有两种发送推送的方式: channel 和“高级定位”。您应该能够使用“高级定位”来指定 deviceToken(如您在示例中所做的那样)。
创建用户安装:
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 请求来更新此条目
发送的方式大致相同。只需将 uri 替换为推送 api,将 json 替换为
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 类
关于java - 使用 Parse 从应用程序引擎发送推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26831405/