gpt4 book ai didi

java - 从应用引擎使用 Parse 发送推送通知

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:45:46 26 4
gpt4 key购买 nike

无法推出我自己的,我决定使用 Parse发送推送通知。我一直在阅读他们的教程。我不清楚如何从 App-Engine 向特定用户发送推送。我正在处理以下情况。 一个给定的用户有五百个 friend 。当所述用户更新她的个人资料图片时,五百个 friend 应该会收到通知。我如何做这么简单的事情?这是非常简单的事情。那么我该如何使用 Parse 做到这一点呢?我的服务器是 Java 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 美元/月的起始费用:这是零推送通知。然后,随着我发送更多推送,这笔钱应该会增加。 (他们只是改变了他们的定价模式)。所以我需要一个替代方案; parse 似乎有很多好处。我只是不知道如何完成我需要的。

最佳答案

Parse 公开了一个 RESTful API,您可以以与您的示例类似的方式使用它(稍作调整)。

当使用 parse 推送通知时,您希望向其发送内容的每个用户都由在 Parse 中注册的“安装”对象表示。您可以在此处找到更多信息。可以通过安装 REST API 对安装执行 CRUD 操作。

他们有两种发送推送的方式: channel 和“高级定位”。您应该能够使用“高级定位”来指定 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 请求来更新此条目

发送的方式大致相同。只需将 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com