Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///necessary data for the post method
- /// this is the token from firebase
- private String serverkey = "key=AAAAa0iy3a8:APA91bG4y2nlRXfTsK0F2Hji2xdIIzboWWB49-TAGQnoHtYekhhSOPqOGy_qipXEwL-
- Ps0smPhzEB1GaZDvXq4McJLZtIfXHRYptErL0Nn7grjB_nv-zkXC0yzMWO6hh2IE5q_i5Ct9O";
- ////this is the url to make the post to
- private String serverUrl = "https://fcm.googleapis.com/fcm/send";
- private void getDetails() {
- requestQueue = VolleySingleton.getInstance(this).getRequestQueue();
- final String api = "https://www.thesportsdb.com/api/v1/json/4013070/latestsoccer.php";
- JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, api, null, response -> {
- try {
- JSONObject jsonObjects = response.getJSONObject("teams");
- JSONArray jsonArray = jsonObjects.getJSONArray("Match");
- for (int i = 0; i < jsonArray.length(); i++) {
- JSONObject jsonObject = jsonArray.getJSONObject(i);
- String league = jsonObject.getString("League");
- if (league.equals("Brasileirão Série B")) {
- /// this is the data that i should post and then retreive to pass the notification
- String datetime = jsonObject.getString("Date");
- String hometeam = jsonObject.getString("HomeTeam");
- String awayteam = jsonObject.getString("AwayTeam");
- String time = jsonObject.getString("Time");
- String date = datetime.substring(0, 10);
- //// subsribe users to a topic
- FirebaseMessaging.getInstance().subscribeToTopic("sport");
- //// making a post request
- JsonObjectRequest jsonObjectRequest1 = new JsonObjectRequest(Request.Method.POST, serverUrl, null, response1 -> {
- OkHttpClient okHttpClient = new OkHttpClient();
- JSONObject jsonObject0 = new JSONObject();
- JSONObject jsonObject1 = new JSONObject();
- try {
- //// the data to post ( date , hometeam,awayteam,time)
- jsonObject1.put("date",date);
- jsonObject1.put("hometeam",hometeam);
- jsonObject1.put("awayteam",awayteam);
- jsonObject1.put("time",time);
- ///// parsing content type
- MediaType mediaType = MediaType.parse("application/json");
- ////setting up the request body which has ( header , authentication key ...)
- RequestBody requestBody = RequestBody.create(mediaType,jsonObject0.toString());
- okhttp3.Request request = new okhttp3.Request.Builder()
- .header("Authorization",serverkey)
- .url("https://fcm.googleapis.com/fcm/send")
- .post(requestBody)
- .build();
- ///// here is the response
- okhttp3.Response okresponse = null;
- try {
- okresponse = okHttpClient.newCall(request).execute();
- String finalResponse = Objects.requireNonNull(okresponse.body()).string();
- Log.d("TODO","final response " + finalResponse);
- } catch (IOException e) {
- e.printStackTrace();
- }
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }, new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- }
- });
- requestQueue.add(jsonObjectRequest1);
- }
- }
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }, error -> {
- });
- requestQueue.add(jsonObjectRequest);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement