【问题标题】:Sending firebase push notification using volley使用 volley 发送 firebase 推送通知
【发布时间】:2018-02-28 05:55:56
【问题描述】:

我想使用 firebase 云消息发送推送通知。 我已成功将访问令牌存储在共享首选项中。

我正在使用 Volley 向服务器发送请求,但它(Volley)在发送请求后显示 com.android.volley.Server 错误

注意:我只是在同一设备上发送 firebase 推送通知,因为在请求正文中传递的访问令牌属于同一(当前)用户

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    String token= Helper.getAccessToken(this);
    if(token!=null){
       sendRequest();
    }

}

private void sendRequest() {
    RequestQueue requestQueue = Volley.newRequestQueue(this);

    String url= "https://fcm.googleapis.com/fcm/send";
    StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_SHORT).show();//Here ServerError shows
        }
    })
    {

        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            HashMap<String,String> params = new HashMap<>();
            String accessToken = Helper.getAccessToken(MainActivity.this);
            params.put("to",accessToken);
            params.put("title", "This is string message");
            return params;
        }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String,String> header = new HashMap<>();
            header.put(""Authorization,"key=" + "Here is my server key");
            header.put("Content-Type","application/json");
            return header;
        }
    }
            ;

            requestQueue.add(request);
}

【问题讨论】:

    标签: android json android-volley firebase-cloud-messaging


    【解决方案1】:

    做一个简单的 POST 操作似乎有点矫枉过正。我认为最好使用 OkHTTP 之类的东西来执行此操作。这应该是一个非常简单的 POST 操作

    private void sendRequestByOk() {
        final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
        new AsyncTask<Void,Void,Void>(){
    
            @Override
            protected Void doInBackground(Void... voids) {
                    OkHttpClient client = new OkHttpClient();
                    JSONObject json = new JSONObject();
                    JSONObject jsonData = new JSONObject();
                    try {
                        jsonData.put("body","Hi!! This is the message from device");
                        jsonData.put("title","dummy title");
                        json.put("notification",jsonData);
                        json.put("to",Helper.getAccessToken(MainActivity.this));
    
                        RequestBody body = RequestBody.create(JSON,json.toString());
                        okhttp3.Request request = new okhttp3.Request.Builder()
                                .header(AUTHORIZATION_KEY,AUTH_VALUE)
                                .url("https://fcm.googleapis.com/fcm/send")
                                .post(body)
                                .build();
    
                        okhttp3.Response response = client.newCall(request).execute();
                        String finalResponse = response.body().string();
                    // Toast.makeText(MainActivity.this, finalResponse,Toast.LENGTH_SHORT).show();
                    } catch (Exception e) {
                        e.printStackTrace();
                    Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                    return  null;
            }
        }.execute();
    }
    

    【讨论】:

    • 我们从哪里获得授权密钥和授权值?
    【解决方案2】:

    我认为下面的代码有问题:-

       @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            HashMap<String,String> params = new HashMap<>();
            String accessToken = Helper.getAccessToken(MainActivity.this);
            params.put("to",accessToken);
       //change this
            params.put("notification", "This is string message");
            return params;
        }
    

    【讨论】:

    • 什么错误?并确保您拥有推送通知的设备令牌。
    • com.android.volley.ServerError,是的,我有推送通知的访问令牌。
    • 去掉get params方法,尝试使用jsonObject发送参数。
    猜你喜欢
    • 2019-03-18
    • 2017-02-24
    • 2017-11-16
    • 1970-01-01
    • 2019-11-11
    • 1970-01-01
    • 1970-01-01
    • 2022-07-16
    • 2022-08-22
    相关资源
    最近更新 更多