向移动设备Volley Firebase发送推送通知

时间:2017-03-17 07:31:49

标签: android firebase push-notification android-volley firebase-cloud-messaging

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());
          }
      }
  ) {

覆盖标头以提供身份验证密钥

  @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);

错误=&gt; com.android.volley.ServerError

我尝试使用POSTMAN发出成功的请求,我需要帮助来更改代码,以便我可以使用齐射来发出相同的请求。

0 个答案:

没有答案