【问题标题】:Spring Boot RestTemplate.postForObject to Firebase Messaging not returningSpring Boot RestTemplate.postForObject 到 Firebase 消息不返回
【发布时间】:2016-12-14 11:38:02
【问题描述】:

我正在尝试将 Google 的 Firebase 消息传递平台绑定到我的应用程序中,并且我正在尝试使用 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。

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

@JsonIgnoreProperties(ignoreUnknown = true)
public class FireBaseResponse {

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

    public FireBaseResponse() {}
}

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

【问题讨论】:

  • 我相信FireBaseResponse 属性名称不符合正确的约定。尝试使用骆驼案例名称(multicastIdcanonicalIds 等:.)。您能否发布您的 Firebase 回复?
  • 您能否处理在客户端收到的这些通知?例如,在func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -&gt; ()) 委托中?
  • 你可以查看这个答案stackoverflow.com/a/51172021/3073945

标签: java spring spring-boot resttemplate firebase-cloud-messaging


【解决方案1】:

试试这样:

    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);
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-01
    • 2018-04-24
    • 2016-08-01
    • 1970-01-01
    • 2021-11-12
    • 2020-11-30
    • 2016-02-02
    相关资源
    最近更新 更多