How To Send Notification From android To Android Using Firebase FCM and Volly
Tags: android
Published: 2023-01-31 10:57:34



```
  implementation 'com.android.volley:volley:1.2.1'
```



First Create Type Notification Object and fill with your custom parameter 


```
 fun createNotificationObject(
        type: String, senderId: String, receiverId: String,
        content: String, senderName: String, media: String,postId:String=""
    ): JSONObject {
        val data = JSONObject()
        data.put("type", type)
        data.put("senderId", senderId)
        data.put("receiverId", receiverId)
        data.put("content",content)
        data.put("senderName",senderName)
        data.put("media", media)
        data.put("postId",postId)
        return data
    }
```


create Type Data Object 

```
  fun createNotificationData(
         type: String,
         senderId: String,
         receiverId: String,
         content: String,
         senderName: String,
         media: String,
         senderImg:String,
         senderToken:String
    ): JSONObject {



        val data = JSONObject()
        data.put("type", type)
        data.put("senderId", senderId)
        data.put("receiverId", receiverId)
        data.put("content",content)
        data.put("senderName",senderName)
        data.put("media", media)
        data.put("senderImg",senderImg)
        data.put("senderToken",senderToken)




       return data
    }
```


send noticification to user token or topic 

```
    fun Push(

        activity: Context?,
        dataValue: JSONObject? /*your data from the activity*/,
        notificationValue :JSONObject?,
        instanceIdToken: String? ,/*firebase instance token you will find in documentation that how to get this*/
        responses:(response:String)->Unit

    ) {
        val url = "https://fcm.googleapis.com/fcm/send"
        val myReq: StringRequest = object : StringRequest(
            Method.POST, url,
            Response.Listener { response ->
                responses(response)
                //Log.e("responseFCM", response)
                //Toast.makeText(activity, "Bingo Success", Toast.LENGTH_SHORT).show();
            },
            Response.ErrorListener {
               // Log.e("responseFCM", it.localizedMessage.toString())

                // Toast.makeText(activity, "Oops error", Toast.LENGTH_SHORT).show();
            }) {

            override fun getBody(): ByteArray {
                val rawParameters: MutableMap<String?, Any?> = Hashtable()
                rawParameters["to"] = instanceIdToken
                rawParameters["priority"] = "high"
                rawParameters["notification"] = notificationValue
                rawParameters["data"] = dataValue
                Log.e("responsess", JSONObject(rawParameters).toString())
                return JSONObject(rawParameters).toString().toByteArray()
            }

            override fun getBodyContentType(): String {
                return "application/json; charset=utf-8"
            }


            override fun getHeaders(): Map<String, String> {
                val headers = HashMap<String, String>()
                headers["Authorization"] =
                    "key=" + "serverId"
                headers["Content-Type"] = "application/json"
                return headers
            }
        }
        Volley.newRequestQueue(activity).add(myReq)
    }
```