Create FCM request class -
@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;
}
}
Create FCM request payload and for sending POST request to FCM, you can use Jersey Rest (JAX-RS) Library like below -
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);