我的服务器在 GAE (Java) 上运行,我正在使用 Urban Airship 服务来传递推送通知。当然,当我使用他们的网络界面发送测试通知时,一切正常,但我想在我的 GAE 应用程序/服务器中添加一个测试按钮,让它触发 UA 发送推送。
问题是,到目前为止我看到的所有示例都不能针对 GAE 的 Java 库进行编译。
有没有人有任何他们想分享的 Java 示例代码,在 GAE 下构建和运行以通过 Urban Airship 触发推送通知?
谢谢!
我的服务器在 GAE (Java) 上运行,我正在使用 Urban Airship 服务来传递推送通知。当然,当我使用他们的网络界面发送测试通知时,一切正常,但我想在我的 GAE 应用程序/服务器中添加一个测试按钮,让它触发 UA 发送推送。
问题是,到目前为止我看到的所有示例都不能针对 GAE 的 Java 库进行编译。
有没有人有任何他们想分享的 Java 示例代码,在 GAE 下构建和运行以通过 Urban Airship 触发推送通知?
谢谢!
下面是一些在 GAE 下工作并通过 Urban Airship 发送推送通知的 Java 示例代码:
URL url = new URL("https://go.urbanairship.com/api/push/");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
String appKey = "YOUR APP KEY HERE";
String appMasterSecret = "YOUR MASTER SECRET HERE";
String authString = appKey + ":" + appMasterSecret;
String authStringBase64 = Base64.encodeBase64String(authString.getBytes());
authStringBase64 = authStringBase64.trim();
connection.setRequestProperty("Content-type", "application/json");
connection.setRequestProperty("Authorization", "Basic " + authStringBase64);
String jsonBodyString = "YOUR URBAN AIRSHIP JSON HERE";
OutputStreamWriter osw = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
osw.write(jsonBodyString);
osw.close();
int responseCode = connection.getResponseCode();
// Add your code to check the response code here
希望这可以帮助!
以防万一有人尝试使用 Google 应用引擎通过 urbanairship 向 iOS 设备发送推送通知,这对我来说终于奏效了!这适用于 V3 API。
导入 org.apache.commons.codec.binary.Base64;//commons-codec-1.6.jar
try {
URL url = new URL(URBAN_AIRSHIP_PUSH_URL);
String nameAndPassword = DEV_API_KEY+":"+DEV_API_MASTER_SECRET;
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;"));
logger.info("Authorization header for push:"+authorizationHeader);
logger.info("PushMessage payload:"+notificationPayload);
request.setPayload(notificationPayload.getBytes("UTF-8"));
URLFetchService urlFetchService = URLFetchServiceFactory.getURLFetchService();
HTTPResponse fetchedResponse = urlFetchService.fetch(request);
if (fetchedResponse.getResponseCode() >= 400) {
logger.warning("Push notification failed:"+new String(fetchedResponse.getContent(), "UTF-8")+
"response code:"+fetchedResponse.getResponseCode());
} else {
logger.info("PushMessage send success");
}
} catch (MalformedURLException e) {
logger.log(Level.SEVERE, "PushMessage failed", e);
} catch (IOException e) {
logger.log(Level.SEVERE, "PushMessage failed", e);
}
因为你说城市飞艇不是必需品,所以我推荐java-apns-gae
。
它是一个开源 Java APNS 库,专为在 Google App Engine 上工作(和使用)而设计。
urbanairship4j(可在 Google Code 上获得)使用Google HTTP Java 客户端,因此可以在 AppEngine、Android 等上完美运行。