0
votes

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))

1

1 Answers

0
votes

Not an appropriate solution but made an end-point in my existing Node project. Any one having this issue can use this. URL https://pankaj-oil-api.herokuapp.com/notify

You need to post this object on above url. "registration_ids" is a string array that can have minimum 1 and maximum 1000 tokens.

{
"registration_ids" : ["Client Token 1","Client Token 2"],
"serverKey": "<Your (Authorization) Server Key Here>",
"title": "<Enter your Notification Title here >",
"msg":"<Enter your Notification Message here >"
}

Code

This is just a temporary solution . Still looking for a solution.