【问题标题】:Push Notification from spring hibernate server to android client从 spring hibernate 服务器推送通知到 android 客户端
【发布时间】:2017-01-22 11:24:00
【问题描述】:

我的项目在 android 和 server 中作为 spring hibernate ,中间件是 Mysql。我想从服务器向客户端发送推送通知。我该怎么做?

【问题讨论】:

    标签: java android hibernate spring-mvc push-notification


    【解决方案1】:

    如何将 Firebase 用作云消息传递服务器。您可以轻松配置和使用它。

    private void sendAndroidNotification(String deviceToken,String message,String title) throws IOException {
            OkHttpClient client = new OkHttpClient();
            MediaType mediaType = MediaType.parse("application/json");
            JSONObject obj = new JSONObject();
            JSONObject msgObject = new JSONObject();
            msgObject.put("body", message);
            msgObject.put("title", title);
            msgObject.put("icon", ANDROID_NOTIFICATION_ICON);
            msgObject.put("color", ANDROID_NOTIFICATION_COLOR);
    
            obj.put("to", deviceToken);
            obj.put("notification",msgObject);
    
            RequestBody body = RequestBody.create(mediaType, obj.toString());
            Request request = new Request.Builder().url(ANDROID_NOTIFICATION_URL).post(body)
                    .addHeader("content-type", CONTENT_TYPE)
                    .addHeader("authorization", "key="+ANDROID_NOTIFICATION_KEY).build();
    
            Response response = client.newCall(request).execute();
            logger.debug("Notification response >>>" +response.body().string());
        }
    

    这个例子有点老见source here,使用official doc作为参考和实施指南。

    【讨论】:

      【解决方案2】:

      您可以使用 [JSR356][1](WebSocket 的 API)的实现。 它集成在最新版本的 tomcat 和 jetty 中。 这是一个例子 [https://gist.github.com/chitan/3063774][2] 。 我很久以前就用过这个实现https://github.com/TooTallNate/Java-WebSocket

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-01-10
        • 2015-09-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-19
        相关资源
        最近更新 更多