Google App Engine(GAE),Urban Airship,Java,推送通知.示例代码?

Oli*_*lie 3 iphone google-app-engine push-notification urbanairship.com

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

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

有没有人有任何他们想要分享构建的Java示例代码并在GAE下运行以通过Urban Airship触发推送通知?

谢谢!

loo*_*mer 6

以下是一些在GAE下工作的Java示例代码,并通过Urban Airship发送推送通知:

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
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!