Advertisement
takieddine

Untitled

Nov 5th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.58 KB | None | 0 0
  1. ///necessary data for the post method
  2.  
  3. /// this is the token from firebase
  4. private String serverkey = "key=AAAAa0iy3a8:APA91bG4y2nlRXfTsK0F2Hji2xdIIzboWWB49-TAGQnoHtYekhhSOPqOGy_qipXEwL-
  5. Ps0smPhzEB1GaZDvXq4McJLZtIfXHRYptErL0Nn7grjB_nv-zkXC0yzMWO6hh2IE5q_i5Ct9O";
  6.  
  7. ////this is the url to make the post to
  8. private String serverUrl = "https://fcm.googleapis.com/fcm/send";
  9.  
  10. private void getDetails() {
  11.  
  12. requestQueue = VolleySingleton.getInstance(this).getRequestQueue();
  13.  
  14. final String api = "https://www.thesportsdb.com/api/v1/json/4013070/latestsoccer.php";
  15.  
  16. JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, api, null, response -> {
  17. try {
  18. JSONObject jsonObjects = response.getJSONObject("teams");
  19. JSONArray jsonArray = jsonObjects.getJSONArray("Match");
  20. for (int i = 0; i < jsonArray.length(); i++) {
  21. JSONObject jsonObject = jsonArray.getJSONObject(i);
  22. String league = jsonObject.getString("League");
  23. if (league.equals("Brasileirão Série B")) {
  24.  
  25. /// this is the data that i should post and then retreive to pass the notification
  26. String datetime = jsonObject.getString("Date");
  27. String hometeam = jsonObject.getString("HomeTeam");
  28. String awayteam = jsonObject.getString("AwayTeam");
  29. String time = jsonObject.getString("Time");
  30. String date = datetime.substring(0, 10);
  31.  
  32. //// subsribe users to a topic
  33. FirebaseMessaging.getInstance().subscribeToTopic("sport");
  34. //// making a post request
  35. JsonObjectRequest jsonObjectRequest1 = new JsonObjectRequest(Request.Method.POST, serverUrl, null, response1 -> {
  36.  
  37. OkHttpClient okHttpClient = new OkHttpClient();
  38. JSONObject jsonObject0 = new JSONObject();
  39. JSONObject jsonObject1 = new JSONObject();
  40. try {
  41. //// the data to post ( date , hometeam,awayteam,time)
  42. jsonObject1.put("date",date);
  43. jsonObject1.put("hometeam",hometeam);
  44. jsonObject1.put("awayteam",awayteam);
  45. jsonObject1.put("time",time);
  46.  
  47. ///// parsing content type
  48. MediaType mediaType = MediaType.parse("application/json");
  49.  
  50. ////setting up the request body which has ( header , authentication key ...)
  51. RequestBody requestBody = RequestBody.create(mediaType,jsonObject0.toString());
  52. okhttp3.Request request = new okhttp3.Request.Builder()
  53. .header("Authorization",serverkey)
  54. .url("https://fcm.googleapis.com/fcm/send")
  55. .post(requestBody)
  56. .build();
  57. ///// here is the response
  58. okhttp3.Response okresponse = null;
  59. try {
  60. okresponse = okHttpClient.newCall(request).execute();
  61. String finalResponse = Objects.requireNonNull(okresponse.body()).string();
  62. Log.d("TODO","final response " + finalResponse);
  63. } catch (IOException e) {
  64. e.printStackTrace();
  65. }
  66.  
  67. } catch (JSONException e) {
  68. e.printStackTrace();
  69. }
  70. }, new Response.ErrorListener() {
  71. @Override
  72. public void onErrorResponse(VolleyError error) {
  73.  
  74. }
  75. });
  76. requestQueue.add(jsonObjectRequest1);
  77. }
  78.  
  79. }
  80. } catch (JSONException e) {
  81. e.printStackTrace();
  82. }
  83.  
  84. }, error -> {
  85.  
  86. });
  87.  
  88. requestQueue.add(jsonObjectRequest);
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement