代码之家  ›  专栏  ›  技术社区  ›  RJB

Firebase从android应用程序发送http云消息

  •  0
  • RJB  · 技术社区  · 6 年前

    我目前正在使用httppost请求从Postman发送firebase云消息。一切正常。我现在正在尝试制作一个简单的Android应用程序来发送来自的消息。但是它不起作用,我也得到了

    https://fcm.googleapis.com/fcm/send

    Key: 'Authorization'   Value: 'key=AAAAnfdQ2jM:AP....'
    
    Key: 'Content-Type'     Value: application/json
    

    然后在体内,使用生的:

    {
    "to": "/topics/anytopic",
    
    
    "data": {
        "my_message": "Hi everyone!",
       }
    
    }
    

    在哪里以及如何将这些信息放入我的volley httppost请求中?

    RequestQueue queue = Volley.newRequestQueue(this);
        StringRequest sr = new StringRequest(Request.Method.POST,"https://fcm.googleapis.com/fcm/send", new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.d(TAG, "Response: " + response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
    
            }
        }){
            @Override
            protected Map<String,String> getParams(){
                Map<String,String> params = new HashMap<String, String>();
                return params;
            }
    
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String,String> params = new HashMap<String, String>();
                return params;
            }
        };
        queue.add(sr);
    

    我找不到这样的例子。

    1 回复  |  直到 6 年前
        1
  •  0
  •   RJB    6 年前

    您需要添加一个JSON主体,如下所示:

            JSONObject jsonBody = new JSONObject();
        jsonBody.put("to", REUVEN_TOKEN_BE_YOUNG);
    
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("my_message", "Keep eating healthy!");
    
        jsonBody.putOpt("data", jsonObject);
    
    
        final String requestBody = jsonBody.toString();
    
    
        RequestQueue queue = Volley.newRequestQueue(this);
        StringRequest sr = new StringRequest(Request.Method.POST,"https://fcm.googleapis.com/fcm/send", new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.d(TAG, "Response: " + response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d(TAG, "That didn't work..." + error);
            }
        }){
            @Override
            protected Map<String,String> getParams(){
                Map<String,String> params = new HashMap<String, String>();
    
                return params;
            }
    
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String,String> params = new HashMap<String, String>();
                params.put("Authorization",BE_YOUNG_APP_KEY);
    
                return params;
            }
    
    
    
            @Override
            public String getBodyContentType() {
                return "application/json; charset=utf-8";
            }
    
            @Override
            public byte[] getBody() throws AuthFailureError {
                try {
                    return requestBody == null ? null : requestBody.getBytes("utf-8");
                } catch (UnsupportedEncodingException uee) {
                    VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", requestBody, "utf-8");
                    return null;
                }
            }
    
        };
        queue.add(sr);