Sendet
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MessageSender.java 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package com.example.erdbebensender;
  2. import android.content.Context;
  3. import android.nfc.Tag;
  4. import android.util.Log;
  5. import com.android.volley.*;
  6. import com.android.volley.toolbox.*;
  7. import org.json.JSONException;
  8. import org.json.JSONObject;
  9. import java.io.*;
  10. import java.net.HttpURLConnection;
  11. import java.net.URL;
  12. import java.nio.charset.StandardCharsets;
  13. public class MessageSender {
  14. private static final String FCM_API = "https://fcm.googleapis.com/fcm/send";
  15. private static final String TOPIC = "erdbeben";
  16. private static final String SERVER_KEY = "AAAA2pVhMk4:APA91bHeiZGBd0BZi-RqnlCqnMkD7ICFW7SKzft-j1RIm16LX4M0Jez8AUQOoGgKYhHsVDD1kDxfKnul9Xbcjwo6QDAnPNLqOnk3XViVI5HjxgL8mpyzDLZXFharMIaqPANqBiMs5Gm2";
  17. private static final String TAG = "ErdbebenMessage";
  18. public void sendMessage(ErdbebenParameter erdbebenParameter) throws IOException, JSONException {
  19. URL url = new URL(FCM_API);
  20. HttpURLConnection con = (HttpURLConnection) url.openConnection();
  21. try {
  22. Log.i(TAG, "Init HTTP Request");
  23. con.setRequestMethod("POST");
  24. con.setRequestProperty("Content-Type", "application/json");
  25. //con.setRequestProperty("Accept", "application/json");
  26. con.setRequestProperty("Authorization", "key=" + SERVER_KEY);
  27. con.setDoOutput(true);
  28. //con.setChunkedStreamingMode(0);
  29. Log.i(TAG, "---Fehlerstelle kommt----");
  30. OutputStream out = new BufferedOutputStream(con.getOutputStream());
  31. Log.i(TAG, "---Fehler behoben---");
  32. out.write("Test".getBytes(),0, "Test".getBytes().length);
  33. out.flush();
  34. out.close();
  35. Log.i(TAG, "Request Erfolgreich gesendet");
  36. try (BufferedReader br = new BufferedReader(
  37. new InputStreamReader(con.getInputStream(), "utf-8"))) {
  38. StringBuilder response = new StringBuilder();
  39. String responseLine = null;
  40. while ((responseLine = br.readLine()) != null) {
  41. response.append(responseLine.trim());
  42. }
  43. Log.i(TAG, "Response erhalten: ");
  44. }
  45. } catch (Exception e) {
  46. throw e;
  47. }
  48. finally {
  49. con.disconnect();
  50. }
  51. }
  52. private JSONObject generateJson(ErdbebenParameter erdbebenParameter) throws JSONException {
  53. Log.i(TAG, "Erstelle JSON");
  54. JSONObject json = new JSONObject();
  55. JSONObject data = new JSONObject();
  56. JSONObject notification = new JSONObject();
  57. notification.put("title", "Erdbeben");
  58. notification.put("body", "Neues Erdbebeben aufgetretten");
  59. data.put("sender", erdbebenParameter.getSender());
  60. data.put("x", erdbebenParameter.getX());
  61. data.put("y", erdbebenParameter.getY());
  62. data.put("v", erdbebenParameter.getV());
  63. json.put("to", "/topics/" + TOPIC);
  64. json.put("notification", notification);
  65. json.put("data", data);
  66. return json;
  67. }
  68. }