错误请求[400],带有翻新的FCM旧版HTTP API [英] Bad Request [400], FCM Legacy HTTP API with Retrofit

查看:199
本文介绍了错误请求[400],带有翻新的FCM旧版HTTP API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

URL = https://fcm.googleapis.com/fcm/send

请求正文

{
"registration_ids": ["token 1","token 2"],
"priority": "high",
"notification": {
"title": "Divine Public School",
"body": "Test Message."
} }

标题

{
"Content-Type: application/json",
"Authorization: key=<myServerKey>"
}

当我从邮递员访问该URL时,我正在获取状态代码200,甚至在客户端应用程序中收到通知.但是,当我尝试在Android中使用Retrofit进行同样的操作时,却收到状态为400错误的请求.

下面的Android代码

interface NotificationService {
@Headers("Content-Type: application/json",
    "Authorization: key=<my server key>")
@POST("fcm/send")
fun sendNotification(@Body body: NotificationBody): Call<ResponseBody> }

数据类

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     )

改造电话

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

NotificationBody对象的日志输出

d/TAG:NotificationBody(registration_ids = [CXD-PHM-QOyLcnLcPozjKA:APA91bGIG-NDG-hSYMlTGWm-ZVaM0hR7Om77CaksvZ4bLDKM0gU_xYk9_Um1aOzPExGR40FeHAqQpkjt_7-HiG8SMPtF5HLrUjCrcD4Asq_ZcEv-Du5AcMthcYjaZjisduLkBPhgPH0b],优先级=越高,通知=通知(标题=神圣公学,身体=你好))

解决方案

这不是合适的解决方案,但在我现有的Node项目中做了一个终点. 任何有此问题的人都可以使用此功能. 网址 https://pankaj-oil-api.herokuapp.com/notify

您需要将此对象发布到上述url上. "registration_ids"是一个字符串数组,可以包含最少1个令牌和最多1000个令牌.

{
"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 >"
}

代码

这只是一个临时解决方案.仍在寻找解决方案.

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

解决方案

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.

这篇关于错误请求[400],带有翻新的FCM旧版HTTP API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆