URL = https://fcm.googleapis.com/fcm/send
Request Body
{
"registration_ids": ["token 1","token 2"],
"priority": "high",
"notification": {
"title": "Divine Public School",
"body": "Test Message."
} }
Header
{
"Content-Type: application/json",
"Authorization: key=<myServerKey>"
}
I am getting status code 200 and even receiving notification in client app when hitting this url from Postman. But when I am trying to do the same in android using retrofit , I am getting status 400 Bad Request.
Android code below
interface NotificationService {
@Headers("Content-Type: application/json",
"Authorization: key=<my server key>")
@POST("fcm/send")
fun sendNotification(@Body body: NotificationBody): Call<ResponseBody> }
Data Class
data class NotificationBody(
@SerializedName("registration_ids")
var registration_ids : ArrayList<String>,
@SerializedName("priority")
var priority:String,
@SerializedName("notification")
var notification:Notification )
data class Notification(
@SerializedName("title")
var title:String,
@SerializedName("body")
var body:String )
Retrofit Call
val generalRetrofit = Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl("https://fcm.googleapis.com/")
.build()!!
val service = generalRetrofit.create(NotificationService::class.java)
val data = NotificationBody(....)
val call = service.sendNotification(data)
call.enqueue(object : Callback<ResponseBody> {
override fun onFailure(call: Call<ResponseBody>, t: Throwable) {}
override fun onResponse(call: Call<ResponseBody>,response: Response<ResponseBody>)
{
Log.d("TAG", response.code().toString())
})
Log output of NotificationBody object
D/TAG: NotificationBody(registration_ids=[cxd-PHM-QOyLcnLcPozjKA:APA91bGIG-NDg-hSYMlTGWm-ZVaM0hR7Om77CaksvZ4bLDKM0gU_xYk9_Um1aOzPExGR40FeHAqQpkjt_7-HiG8SMPtF5HLrUjCrcD4Asq_ZcEv-Du5AcMthcYjaZjisduLkBPhgPH0b], priority=higher, notification=Notification(title=Divine Public School, body=Hello))