Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring Boot RestTemplate.postForObject到Firebase消息传递不返回_Java_Spring_Spring Boot_Resttemplate_Firebase Cloud Messaging - Fatal编程技术网

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

Java Spring Boot RestTemplate.postForObject到Firebase消息传递不返回,java,spring,spring-boot,resttemplate,firebase-cloud-messaging,Java,Spring,Spring Boot,Resttemplate,Firebase Cloud Messaging,我试图将Google的Firebase消息平台与我的应用程序联系起来,并尝试使用Spring内置的REST模板REST抽象来简化它 我目前正在尝试: RestTemplate restTemplate = new RestTemplate(); restTemplate.getMessageConverters().add(new GsonHttpMessageConverter()); MultiValueMap<String, String> headers = new Lin

我试图将Google的Firebase消息平台与我的应用程序联系起来,并尝试使用Spring内置的REST模板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);
我很难理解为什么这个电话永远不会结束。我希望能够将响应直接输入到对象中。

尝试如下:

    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);
        }
    }
包你的包;
导入com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown=true)
公共类FirebaseResponse{
专用长组播标识;
私人整数成功;
私有整数故障;
私有对象规范标识;
公共FirebaseResponse(){
}
//----用这个----
公共布尔值是_success(){
如果(getSuccess()==1){
返回true;
}否则{
返回false;
}
}
public long getMulticast_id(){
返回多播地址;
}
public void setMulticast_id(长multicast_id){
this.multicast\u id=multicast\u id;
}
公共整数getSuccess(){
回归成功;
}
public void setSuccess(整数成功){
成功=成功;
}
公共整数getFailure(){
返回失败;
}
公共无效设置失败(整数失败){
失败=失败;
}
公共对象getCanonical_id(){
返回规范标识;
}
public void setCanonical_id(对象规范_id){
this.canonical_id=canonical_id;
}
@凌驾
公共字符串toString(){
返回“FirebaseResponse{”+
“multicast_id=“+multicast_id+
“,success=“+success+
“,failure=“+failure+
“,canonical_id=“+canonical_id+
'}';
}
}
//---------------用法------------------
ArrayList拦截器=新的ArrayList();
添加(新的HeaderRequestInterceptor(“授权”、“密钥=“+FIREBASE_服务器_密钥”);
add(新的HeaderRequestInterceptor(“内容类型”、“应用程序/json”);
restTemplate.setInterceptors(拦截器);
JSONObject body=新的JSONObject();
//JsonArray注册_id=new JsonArray();
//body.put(“注册号”,注册号);
正文.付诸表决(“至”,“CFW930CZXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXIPDO-BJlACHRQZC0ASXLRFKDMHV”和BXKRZLZLXXXXXXXXXXXXX59SPW4RW-5MTWKKZXXXXXXXXXGXLL-LLIJUJPWZPLGLPJI”);
正文。put(“优先级”、“高”);
//body.put(“dry_run”,true);
JSONObject通知=新建JSONObject();
通知。放置(“正文”、“正文字符串在此”);
通知。放置(“标题”,“此处标题字符串”);
//通知。放置(“图标”、“我的图标”);
JSONObject数据=新的JSONObject();
数据输入(“键1”、“值1”);
数据。put(“键2”、“值2”);
正文.付诸表决(“通知”,通知);
body.put(“数据”,data);
HttpEntity请求=新的HttpEntity(body.toString());
FirebaseResponse FirebaseResponse=restTemplate.postForObject(“https://fcm.googleapis.com/fcm/send,请求,FirebaseResponse.class);
log.info(“响应为:”+firebaseress.toString());
返回新的ResponseEntity(firebaseResponse.toString(),HttpStatus.OK);
//---------------助手类------------------
导入org.springframework.http.HttpRequest;
导入org.springframework.http.client.ClientHttpRequestExecution;
导入org.springframework.http.client.clienthttpprequestinterceptor;
导入org.springframework.http.client.ClientHttpResponse;
导入org.springframework.http.client.support.HttpRequestWrapper;
导入java.io.IOException;
公共类HeaderRequestInterceptor实现ClientHttpRequestInterceptor{
私有最终字符串头名称;
私有最终字符串头值;
public HeaderRequestInterceptor(字符串headerName、字符串headerValue){
this.headerName=headerName;
this.headerValue=headerValue;
}
@凌驾
公共ClientHttpResponse截获(HttpRequest请求,字节[]正文,ClientHttpPrequesteExecution执行)引发IOException{
HttpRequest包装器=新的HttpRequestWrapper(请求);
wrapper.getHeaders().set(headerName,headerValue);
返回execution.execute(包装器,主体);
}
}

我认为
FireBaseResponse
属性名称没有遵循正确的惯例。尝试使用驼峰大小写名称(
multicastId
canonicalid
,等等:)。你能发布你的Firebase回复吗?你能处理客户端收到的这些通知吗?例如,在
func应用程序(application:UIApplication,DidReceiveMemoteNotification用户信息:[AnyHashable:Any],fetchCompletionHandler completionHandler:@escaping(UIBackgroundFetchResult)->())
委托中,您可以检查此答案
    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);
        }
    }