现在我正在尝试在我的Java程序中实现Firebase云消息传递。通过FCM发送推送通知。现在,FCM告诉我send it like an HTTP POST request。
这是我要发送的请求:
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..."
}
如何从Eclipse Java程序发送此文件?
答案 0 :(得分:3)
使用httpclient非常容易,这是示例
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;
}
}
答案 1 :(得分:0)
创建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请求有效负载并向FCM发送POST请求,您可以使用如下所示的Jersey Rest(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);