繁体   English   中英

Spring Boot RestTemplate.postForObject 到 Firebase 消息不返回

[英]Spring Boot RestTemplate.postForObject to Firebase Messaging not returning

我正在尝试将 Google 的 Firebase Messaging 平台绑定到我的应用程序中,并且我正在尝试使用 Spring 内置的 RestTemplate REST 抽象来简化它。

我目前正在尝试:

RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new GsonHttpMessageConverter());

MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
headers.add("Authorization", "key=" + Constants.FIREBASE_SERVER_KEY);
headers.add("Content-Type", "application/json");

HttpEntity<FireBasePost> entity = new HttpEntity<>(fbp, headers);
URI uri;
uri = new URI(firebaseApi);

FireBaseResponse fbr = restTemplate.postForObject(uri, entity, FireBaseResponse.class);

FireBasePost 对象仅包含 POST 消息 API 所需的字段: Firebase API - 我已经通过使用 String.class 发布来验证请求实体是否有效,因此响应是未编组的 JSON。

但是,在尝试将响应直接编组到 FireBaseResponse 对象中时,对 postForObject 的调用挂起并且永远不会返回。

@JsonIgnoreProperties(ignoreUnknown = true)
public class FireBaseResponse {

    public Integer multicast_id;
    public Integer success;
    public Integer failure;
    public Integer canonical_ids;

    public FireBaseResponse() {}
}

我无法理解为什么这个调用永远不会完成。 我希望能够将响应直接放入一个对象中。

试试这样:

    package yourpackage;

    import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

    @JsonIgnoreProperties(ignoreUnknown = true)
    public class FirebaseResponse {

        private long multicast_id;
        private Integer success;
        private Integer failure;
        private Object canonical_ids;

        public FirebaseResponse() {
        }

        //---- use this one ----
        public boolean is_success() {
            if (getSuccess() == 1) {
                return true;
            } else {
                return false;
            }
        }

        public long getMulticast_id() {
            return multicast_id;
        }

        public void setMulticast_id(long multicast_id) {
            this.multicast_id = multicast_id;
        }

        public Integer getSuccess() {
            return success;
        }

        public void setSuccess(Integer success) {
            this.success = success;
        }

        public Integer getFailure() {
            return failure;
        }

        public void setFailure(Integer failure) {
            this.failure = failure;
        }

        public Object getCanonical_ids() {
            return canonical_ids;
        }

        public void setCanonical_ids(Object canonical_ids) {
            this.canonical_ids = canonical_ids;
        }

        @Override
        public String toString() {
            return "FirebaseResponse{" +
                    "multicast_id=" + multicast_id +
                    ", success=" + success +
                    ", failure=" + failure +
                    ", canonical_ids=" + canonical_ids +
                    '}';
        }
    }

//--------------- USAGE ------------------
                ArrayList<ClientHttpRequestInterceptor> interceptors = new ArrayList<>();
            interceptors.add(new HeaderRequestInterceptor("Authorization", "key=" + FIREBASE_SERVER_KEY));
            interceptors.add(new HeaderRequestInterceptor("Content-Type", "application/json"));
            restTemplate.setInterceptors(interceptors);


        JSONObject body = new JSONObject();
            // JsonArray registration_ids = new JsonArray();
            // body.put("registration_ids", registration_ids);
            body.put("to", "cfW930CZxxxxxxxxxxxxxxxxxxxxxxxxxxipdO-bjHLacHRqQzC0aSXlRFKdMHv_aNBxkRZLNxxxxxxxxxxx59sPW4Rw-5MtwKkZxxxxxxxgXlL-LliJuujPwZpLgLpji_");
            body.put("priority", "high");
            // body.put("dry_run", true);

            JSONObject notification = new JSONObject();
            notification.put("body", "body string here");
            notification.put("title", "title string here");
            // notification.put("icon", "myicon");

            JSONObject data = new JSONObject();
            data.put("key1", "value1");
            data.put("key2", "value2");

            body.put("notification", notification);
            body.put("data", data);



            HttpEntity<String> request = new HttpEntity<>(body.toString());

            FirebaseResponse firebaseResponse = restTemplate.postForObject("https://fcm.googleapis.com/fcm/send", request, FirebaseResponse.class);
            log.info("response is: " + firebaseResponse.toString());


            return new ResponseEntity<>(firebaseResponse.toString(), HttpStatus.OK);

//--------------- HELPER CLASS ------------------    

    import org.springframework.http.HttpRequest;
    import org.springframework.http.client.ClientHttpRequestExecution;
    import org.springframework.http.client.ClientHttpRequestInterceptor;
    import org.springframework.http.client.ClientHttpResponse;
    import org.springframework.http.client.support.HttpRequestWrapper;

    import java.io.IOException;

    public class HeaderRequestInterceptor implements ClientHttpRequestInterceptor {

        private final String headerName;

        private final String headerValue;

        public HeaderRequestInterceptor(String headerName, String headerValue) {
            this.headerName = headerName;
            this.headerValue = headerValue;
        }

        @Override
        public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
            HttpRequest wrapper = new HttpRequestWrapper(request);
            wrapper.getHeaders().set(headerName, headerValue);
            return execution.execute(wrapper, body);
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM