java - 在 Netbeans 中成功发送通知后如何获得 Firebase 响应?

标签 java json firebase netbeans

我正在尝试为我的 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);
}

最佳答案

我刚刚使用 BufferedReader 在这里找到了答案。因为我不知道如何从 firebase 获取 json 响应的确切名称...

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

关于java - 在 Netbeans 中成功发送通知后如何获得 Firebase 响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56123569/

相关文章:

javascript - 从变量访问 JSON 值

javascript - highchart json 数据点

json - JSON 字段 POSTGRESQL 中的单引号

java - com.google.firebase.database.DatabaseException : Can't convert object of type java. lang.String 要输入

java - 如果推理器一致且发生更改,则 OWL API 保存本体

java - DSA(数字签名算法)实现 - key 生成

android - 通过向查询添加监听器返回的 DataSnapshot 是否不同于直接添加到 DatabaseReference 的 DataSnapshot

angularjs - Angular 绑定(bind)无法与 ngInfiniteScroll 正常工作

java - 如何让这个脉冲波在Java中的占空比样本之间有更精细的转换

java - Android Studio 构建错误提示 JVM 版本