java - 在 Netbeans 中成功发送通知后如何获得 Firebase 响应?
问题描述
我正在尝试制作我的 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响应的确切名称......
推荐阅读
- react-native - 深度链接 - 如果应用程序关闭则不起作用
- c# - 我需要使用 regex (Address) C# 替换一些单词
- java - 如何读取 SOAP 请求正文元素而不在 Java SpringWS 中的 xsd 或 wsdl 中声明它?
- opencv - 如何在 google colab 中使用 opencv 显示 mp4
- go - Kafka:Sarama、幂等性和 transactional.id
- r - 我可以在 ggplot 条形图中选择要着色的特定行吗?
- google-analytics - Google Analytics(分析)目标转化限制
- python - 大小为 200 和 300 的两个数组。如何连接数组以使其成为 200x300?
- groovy - 字符串中的对象名称 - 如何使用它
- c# - 如何在已部署的 WPF 应用程序中使用谷歌语音文本 api 密钥?