【问题标题】:Send push notification with Parse from app-engine使用 Parse 从 app-engine 发送推送通知
【发布时间】:2015-01-06 00:36:13
【问题描述】:

无法推出我自己的,我决定使用Parse 发送推送通知。我一直在阅读他们的教程。我不清楚如何从 App-Engine 向特定用户发送推送。我正在处理以下情况。 一个给定的用户有五百个朋友。当该用户更新她的个人资料图片时,五百个朋友应该会收到通知。 我该怎么做这么简单的事情?这是非常简单的东西。那么我该如何用 Parse 做到这一点呢?我的服务器是 Java App-engine。我需要知道如何处理应用引擎部分。 (顺便说一句:我已经成功实现了 app-engine to android push)。

在某些情况下,这是我在 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 似乎很划算。我只是不知道如何完成我需要的。

【问题讨论】:

    标签: java iphone google-app-engine parse-platform push-notification


    【解决方案1】:

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

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

    他们有两种发送推送的方式:渠道和“高级定位”。您应该能够使用“高级定位”来指定 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替换为push 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 编写的吗?我正在使用 Java。
    • 对不起。那里离题太远了。我将更新代码示例。这个概念应该是一样的。我以为您使用的是特定库(基于您的代码示例)。
    • 代码还是有很多bug的。但它为我指明了正确的方向。非常感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多