Reputation: 1878
Now I am trying to implement Firebase Cloud Messaging in my Java program. To send push-notifications through FCM. Now, FCM tells me to send it like an HTTP POST request.
This is the request I am trying to send:
https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA
{ "data": {
"score": "5x1",
"time": "15:10"
},
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
}
How do I send this from my Eclipse Java program?
Upvotes: 1
Views: 5434
Reputation: 4427
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);
Upvotes: 0
Reputation: 56
It is very easy with httpclient, here is example
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
public class HttpClientUtils {
public JSONObject doPost() {
HttpClient httpclient = HttpClientBuilder.create().build();
HttpPost request = new HttpPost("https://fcm.googleapis.com/fcm/send");
JSONObject result = new JSONObject();
try {
String bodyContent = "{ 'data': { 'score': '5x1', 'time': '15:10' }, 'to' : 'bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...'}";
StringEntity requestBody = new StringEntity(bodyContent);
request.setEntity(requestBody);
request.setHeader("Content-Type", "application/json");
request.setHeader("Authorization", "key=AIzaSyZ-1u...0GBYzPu7Udno5aA");
HttpResponse response = httpclient.execute(request);
HttpEntity entity = response.getEntity();
String responseString = EntityUtils.toString(entity);
result.put("status", response.getStatusLine().getStatusCode());
result.put("bodyContent", new JSONObject(responseString));
} catch (ClientProtocolException e) {
result.put("status", "500");
result.put("bodyContent", "");
e.printStackTrace();
} catch (IOException e) {
result.put("status", "500");
result.put("bodyContent", "");
e.printStackTrace();
} finally {
request.releaseConnection();
}
return result;
}
}
Upvotes: 3