【问题标题】:Bad Request [400], FCM Legacy HTTP API with Retrofit错误请求 [400],带有改造的 FCM 旧版 HTTP API
【发布时间】:2020-09-02 04:19:13
【问题描述】:

网址 = 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>"
}

从 Postman 访问此 URL 时,我收到状态代码 200,甚至在客户端应用程序中收到通知。但是当我尝试在 android 中使用 retrofit 做同样的事情时,我得到状态 400 Bad Request。

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],优先级=越高,通知=通知(标题=神圣公学,身体=你好))

【问题讨论】:

    标签: android firebase firebase-cloud-messaging retrofit retrofit2


    【解决方案1】:

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

    您需要在上面的网址上发布此对象。 "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 >"
    }
    

    Code

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

    【讨论】:

      猜你喜欢
      • 2018-11-20
      • 2014-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-21
      相关资源
      最近更新 更多