我正在尝试创建Firebase消息传递应用程序的后端,如何在成功发送通知后如何在Java Netbeans中获得Firebase响应。
如何从Firebase获取json响应?
public class SamplePhpJava {
public static final String FCM_AUTH_KEY = "asdasdasdasdasdasdadfasdfSample";
public static final String FCM_SEND_API = "https://fcm.googleapis.com/fcm/send";
public static final String REQUEST_METHOD = "POST";
public static final String TOPIC = "sfdgsdfgsfgvadfaefdasdSAmple";
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception{
// TODO code application logic here
pushFcmNotification();
JSONObject jSONObject = (JSONObject) getResponse("firebase.json");
System.out.println(jSONObject);
}
private static void pushFcmNotification() throws Exception{
URL url = new URL(FCM_SEND_API);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod(REQUEST_METHOD);
conn.setRequestProperty("Authorization", "key="+FCM_AUTH_KEY);
conn.setRequestProperty("Content-Type", "application/json");
JSONObject json = new JSONObject();
json.put("to",TOPIC.trim());
JSONObject info = new JSONObject();
info.put("title", "Notification Title");
info.put("body", "Hello Test Notification");
json.put("notification", info);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(json.toString());
wr.flush();
conn.getInputStream();
}
private static Object getResponse(String response) throws Exception{
FileReader reader = new FileReader(response);
JSONParser jSONParser = new JSONParser();
return jSONParser.parse(reader);
}
答案 0 :(得分:0)
我刚刚在这里使用BufferedReader找到了答案。由于我不知道如何从Firebase获取json响应的确切名称...