創建FCM請求類 -
@JsonInclude(JsonInclude.Include.NON_NULL)
public static class FCMRequest {
@JsonProperty("type")
private String type;
@JsonProperty("expiry")
private String expiry;
@JsonProperty("body")
private String body;
@JsonProperty("title")
private String title;
@JsonProperty("subscriberId")
private String subscriberId;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getExpiry() {
return expiry;
}
public void setExpiry(String expiry) {
this.expiry = expiry;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getSubscriberId() {
return subscriberId;
}
public void setSubscriberId(String subscriberId) {
this.subscriberId = subscriberId;
}
}
創建FCM請求負載和發送POST請求FCM,您可以用球衣休息(JAX-RS)庫如下 -
FCMRequest fcmRequest = new FCMRequest();
//set the rquest data
WebTarget target = client.target("https://fcm.googleapis.com/fcm/send");
Entity<FCMRequest> entity = Entity.entity(fcmRequest, MediaType.APPLICATION_JSON_TYPE);
Response authResponse = target.request()
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
.header(HttpHeaders.AUTHORIZATION, KEY)
.post(entity, Response.class);
這可能會有所幫助:http://stackoverflow.com/questions/4205980/java-s結束-http-parameters-via-post-method-easily – JitterbugChew
@JitterbugChew我無法準確理解HTTP請求在那個答案中。我應該在那裏寫我的代碼。我應該用我的什麼代碼替換.. – Peter
你已經有'key = AIzaSyZ-1u ... 0GBYzPu7Udno5aA'嗎? –