我試圖創建一個Java應用程序應該發送協議到火力地堡雲端通訊服務器。 現在我有這個問題,我不知道如何看我的郵件是否正確發送。當我前往網絡控制檯的Firebase /通知有沒有消息看到。火力地堡雲消息發送HTTP-POST在Java中
我的代碼(以防萬一):
import org.apache.http.HttpResponse;
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.json.JSONObject;
public class Mainclass {
public static void main(String[] args) {
// Declaration of Message Parameters
String message_url = new String("https://fcm.googleapis.com/fcm/send");
String message_sender_id = new String("XXXX-XXXX");
String message_key = new String("key=XXXX-XXXX");
// Generating a JSONObject for the content of the message
JSONObject message = new JSONObject();
message.put("message", "TEXT");
JSONObject protocol = new JSONObject();
protocol.put("to", message_sender_id);
protocol.put("data", message);
// Send Protocol
try {
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost request = new HttpPost(message_url);
request.addHeader("content-type", "application/json");
request.addHeader("Authorization", message_key);
StringEntity params = new StringEntity(protocol.toString());
request.setEntity(params);
System.out.println(params);
HttpResponse response = httpClient.execute(request);
System.out.println(response.toString());
} catch (Exception e) {
}
}
}
輸出:
[Content-Type: text/plain; charset=ISO-8859-1,Content-Length: 59,Chunked: false]
HttpResponseProxy{HTTP/1.1 200 OK [Content-Type: application/json; charset=UTF-8, Date: Mon, 26 Dec 2016 12:09:13 GMT, Expires: Mon, 26 Dec 2016 12:09:13 GMT, Cache-Control: private, max-age=0, X-Content-Type-Options: nosniff, X-Frame-Options: SAMEORIGIN, X-XSS-Protection: 1; mode=block, Server: GSE, Alt-Svc: quic=":443"; ma=2592000; v="35,34", Transfer-Encoding: chunked] [email protected]}
謝謝!
PS有FCM https://firebase.google.com/docs/cloud-messaging/server
嘿,我也在嘗試同樣的事情。現在我正在嘗試一種不同的方式來發送它。如果成功,我會成功:1種消息。 ..然後我回復。 – Debasish