2

Google의 Firebase Messaging 플랫폼을 내 앱에 연결하려고합니다.이를 단순화하기 위해 Spring의 RestTemplate REST 추상화를 사용하려고합니다.스프링 부트 RestTemplate.postForObject를 Firebase 메시징으로 반환하지 않습니다.

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 객체에 직접 marshall에 대한 응답을 얻으려고 시도하면 postForObject 호출이 중지되어 반환되지 않습니다.

@JsonIgnoreProperties(ignoreUnknown = true) 
public class FireBaseResponse { 

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

    public FireBaseResponse() {} 
} 

이 호출이 완료되지 않는 이유를 이해하는 데 문제가 있습니다. 나는 객체에 직접 응답 할 수 있기를 원합니다.

+0

내가 FireBaseResponse' 속성 이름은 바로 규칙을 따르지 않는'생각합니다. 낙타의 경우 이름으로 시도하십시오 ('multicastId','canonicalIds' 등). Firebase 응답을 게시 할 수 있습니까? –

+0

클라이언트 측에서받은 알림을 처리 할 수 ​​있습니까? 예를 들어, func 어플리케이션 (_ application : UIApplication, didReceiveRemoteNotification userInfo : [AnyHashable : Any], fetchCompletionHandler completionHandler : @escaping (UIBackgroundFetchResult) ->())'delegate? – Dayna

답변

0

이 같은 시도 :

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); 
     } 
    } 
관련 문제