2

Ich versuche, die Firebase Messaging-Plattform von Google mit meiner App zu verknüpfen, und ich versuche, die in RestTemplate REST integrierte Abstraktion von Spring zu verwenden, um sie zu vereinfachen.Spring Boot RestTemplate.postForObject zu Firebase Messaging wird nicht zurückgegeben

ich zur Zeit bin versucht zu:

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

Das FireBasePost Objekt enthält nur die erforderlichen Felder für die POST-Meldung API: Firebase API - und ich habe die Anfrage Entity prüft funktioniert, indem mit String.class Entsendung, so Die Antwort lautet JSON.

Wenn jedoch versucht wird, die Antwort auf Marshall direkt in das FireBaseResponse-Objekt zu erhalten, bleibt der Aufruf von postForObject hängen und kehrt nie zurück.

Ich habe Probleme zu verstehen, warum dieser Anruf nie abgeschlossen wird. Ich würde gerne in der Lage sein, die Antwort direkt in ein Objekt zu bekommen.

+0

Ich glaube 'FireBaseResponse' Eigenschaftsnamen folgen nicht richtig Konvention. Versuchen Sie es mit Camel Case-Namen ('multicastId',' canonicalIds', etc:). Kannst du bitte deine Firebase-Antwort posten? –

+0

Können Sie diese auf der Clientseite empfangenen Benachrichtigungen verarbeiten? Zum Beispiel in der 'func-Anwendung (_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Beliebige], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) ->())' delegieren? – Dayna

Antwort

0

versuchen wie folgt aus:

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