How to send device to device messages using Firebase Cloud Messaging?

后端 未结 13 2185
Happy的楠姐
Happy的楠姐 2020-11-22 01:47

After searching the docs I could not find any info on how to send device to device messages using FCM without the use of an external server.

For example, if I was cr

13条回答
  •  佛祖请我去吃肉
    2020-11-22 02:11

    You can do it using Volly Jsonobject request....

    follow this Steps first:

    1 copy legacy server key and store it as Legacy_SERVER_KEY

    Legacy Server key

    you can see in picture how to get

    2 You need Volley dependency

    compile 'com.mcxiaoke.volley:library:1.0.19'

    Code for send Push:-

    private void sendFCMPush() {
    
        String Legacy_SERVER_KEY = YOUR_Legacy_SERVER_KEY;
        String msg = "this is test message,.,,.,.";
        String title = "my title";
        String token = FCM_RECEIVER_TOKEN;
    
        JSONObject obj = null;
        JSONObject objData = null;
        JSONObject dataobjData = null;
    
        try {
            obj = new JSONObject();
            objData = new JSONObject();
    
            objData.put("body", msg);
            objData.put("title", title);
            objData.put("sound", "default");
            objData.put("icon", "icon_name"); //   icon_name image must be there in drawable
            objData.put("tag", token);
            objData.put("priority", "high");
    
            dataobjData = new JSONObject();
            dataobjData.put("text", msg);
            dataobjData.put("title", title);
    
            obj.put("to", token);
            //obj.put("priority", "high");
    
            obj.put("notification", objData);
            obj.put("data", dataobjData);
            Log.e("!_@rj@_@@_PASS:>", obj.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
    
        JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST, Constants.FCM_PUSH_URL, obj,
                new Response.Listener() {
                    @Override
                    public void onResponse(JSONObject response) {
                        Log.e("!_@@_SUCESS", response + "");
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.e("!_@@_Errors--", error + "");
                    }
                }) {
            @Override
            public Map getHeaders() throws AuthFailureError {
                Map params = new HashMap();
                params.put("Authorization", "key=" + Legacy_SERVER_KEY);
                params.put("Content-Type", "application/json");
                return params;
            }
        };
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        int socketTimeout = 1000 * 60;// 60 seconds
        RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
        jsObjRequest.setRetryPolicy(policy);
        requestQueue.add(jsObjRequest);
    }
    

    Just Call sendFCMPush();

提交回复
热议问题