How can I send a Firebase Cloud Messaging notification without use the Firebase Console?

后端 未结 16 1292
南旧
南旧 2020-11-22 10:17

I\'m starting with the new Google service for the notifications, Firebase Cloud Messaging.

Thanks to this code https://github.com/firebase/quickstart-a

16条回答
  •  渐次进展
    2020-11-22 10:44

    If you want to send push notifications from android check out my blog post

    Send Push Notifications from 1 android phone to another with out server.

    sending push notification is nothing but a post request to https://fcm.googleapis.com/fcm/send

    code snippet using volley:

        JSONObject json = new JSONObject();
     try {
     JSONObject userData=new JSONObject();
     userData.put("title","your title");
     userData.put("body","your body");
    
    json.put("data",userData);
    json.put("to", receiverFirebaseToken);
     }
     catch (JSONException e) {
     e.printStackTrace();
     }
    
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest("https://fcm.googleapis.com/fcm/send", json, new Response.Listener() {
     @Override
     public void onResponse(JSONObject response) {
    
    Log.i("onResponse", "" + response.toString());
     }
     }, new Response.ErrorListener() {
     @Override
     public void onErrorResponse(VolleyError error) {
    
    }
     }) {
     @Override
     public Map getHeaders() throws AuthFailureError {
    
    Map params = new HashMap();
     params.put("Authorizationey=" + SERVER_API_KEY);
     params.put("Content-Typepplication/json");
     return params;
     }
     };
     MySingleton.getInstance(context).addToRequestQueue(jsonObjectRequest);
    

    I suggest you all to check out my blog post for complete details.

提交回复
热议问题