我的服务器在 GAE (Java) 上运行,我正在使用 Urban Airship 服务来提供推送通知。当然,当我使用他们的网络界面发送测试通知时一切正常,但我想在我的 GAE 应用程序/服务器中添加一个测试按钮,让它触发 UA 发送推送。

问题是,到目前为止我看到的所有示例都不能针对 GAE 的 Java 库进行编译。

有没有人想要分享在 GAE 下构建和运行的 Java 示例代码,以通过 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

希望这可以帮助!

关于iphone - Google App Engine (GAE)、Urban Airship、Java、推送通知。示例代码?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3689108/

10-14 15:13