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

发布于 2024-09-18 07:28:56 字数 269 浏览 4 评论 0原文

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

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

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

谢谢!

My server is running on GAE (Java), and I'm using Urban Airship service to deliver push notifications. Of course, everything works fine when I use their web-interface to send a test notification, but I'd like to add a test-button to my GAE app/server to have it trigger UA to send the push.

The problem is, all of the examples I've seen so far don't compile against GAE's Java libraries.

Does anyone have any java sample code they'd like to share that build & runs under GAE to trigger a push notification through Urban Airship?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

晌融 2024-09-25 07:28:56

以下是一些在 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

希望这会有所帮助!

Here is some Java sample code that works under GAE and sends a push notification through 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

Hope this helps!

人间☆小暴躁 2024-09-25 07:28:56

以防万一有人尝试使用 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);
        }

Just in case anyone is trying to use Google app engine to send push notification via urbanairship, to iOS devices, this is what finally worked for me! This is for V3 API.

import 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);
        }
等风也等你 2024-09-25 07:28:56

因为您说城市飞艇不是必需的,所以我推荐java-apns-gae

它是一个开源 Java APNS 库,专门设计用于在 Google App Engine 上工作(和使用)。

https://github.com/ZsoltSafrany/java-apns-gae

Because you said Urban Airship isn't a requirement let me recommend java-apns-gae.

It's an open-source Java APNS library that was specifically designed to work (and be used) on Google App Engine.

https://github.com/ZsoltSafrany/java-apns-gae

浅唱々樱花落 2024-09-25 07:28:56

urbanairship4j(可通过 Google 代码获取)使用 Google HTTP Java 客户端 因此可以在 AppEngine、Android 等上完美运行。

urbanairship4j (available on Google Code) uses Google HTTP Java Client so works perfectly on AppEngine, Android, etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文