0

Code for Volley JsonObjectRequest

RequestQueue queue = Volley.newRequestQueue(context);
String url = "https://fcm.googleapis.com/fcm/send";
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST, url, null,
      new Response.Listener<JSONObject>()
      {
          @Override
          public void onResponse(JSONObject response) {
              Log.d("ERROR-->", response.toString());
          }
      },
      new ErrorListener()
      {
          @Override
          public void onErrorResponse(VolleyError error) {
              // TODO Auto-generated method stub
              Log.d("ERROR-->","error => "+error.toString());
          }
      }
  ) {

Overriding header to provide authentication key

  @Override
  public Map<String, String> getHeaders() throws AuthFailureError {
      Map<String, String>  params = new HashMap<String, String>();
      params.put("Authorization", "key=AIzaSyB_xxxxx_xxxxxxxxxxxxxxxx");
      params.put("Content-Type", "application/json");
      String mapAsJson = null;
      try {
          mapAsJson = new ObjectMapper().writeValueAsString(params);
      } catch (JsonProcessingException e) {
          e.printStackTrace();
      }
      //Log.d("ERROR",""+mapAsJson);
      Log.d("ERROR","-header-->"+params);
      return params;
  }
  @Override
  protected Map<String,String> getParams() throws AuthFailureError{
      Map<String,String> params = new HashMap<String, String>();
      Map<String,String> parent  = new HashMap<String, String>();
      Map<String,String> child = new HashMap<String, String>();
     /* {
          "notification":{
              "title":"Mandeep ",
              "text": "hello"
      },
          "to": "/topics/mandeep"
      }*/
      parent.put("title","Mandeep");
      parent.put("text","Push Notification");
      params.put("notification", String.valueOf(parent));
      //params.put("", String.valueOf(child));
      params.put("to","/topics/mandeep");
      JSONObject obj=new JSONObject(params);
      JSONArray array = null;
      try {
          array=new JSONArray(obj.toString());
      } catch (JSONException e) {
          e.printStackTrace();
      }
      //child.put("", String.valueOf(params));
      Log.d("ERROR","-msg-->"+params);
      return params;
  }

  @Override
  public String getBodyContentType() {
      return "application/json";
  }
};
queue.add(jsonObjReq);

Error => com.android.volley.ServerError

I tried to make successful requests using POSTMAN and I need help to change the code so that I can make the same request using volley.

KENdi
  • 7,576
  • 2
  • 16
  • 31
khalil
  • 5
  • 8
  • http://stackoverflow.com/questions/37435750/how-to-send-device-to-device-messages-using-firebase-cloud-messaging/42452064#42452064 – Rajesh Satvara Mar 17 '17 at 07:36
  • 1
    Hi khalil. It is ***strongly*** advised not to send *downstream* messages from the Android devices itself, since it requires you to include the *Server Key* in the client app code, which exposes it to unauthorized users to exploit. – AL. Mar 17 '17 at 07:36
  • looking for solution to this. did you ever solve this? – Manny265 May 27 '18 at 13:35

0 Answers0