简体   繁体   中英

How to get a Firebase response after a successful sending a notification in Netbeans?

I'm trying to make a backend of my Firebase messaging app, how to get a firebase response in Java Netbeans after successfully sending a notification.

How can I get the json response from Firebase?

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);
}

i just found my answer here using BufferedReader. Since i dont know how to get the exact name of json response from firebase...

https://en.proft.me/2013/12/5/how-parse-json-java/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM