坏的请求[400],FCM传统HTTP API与改造

问题描述 投票:1回答:1

URL = https:/fcm.googleapis.comfcmsend

要求正文

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

头部

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

我得到的状态代码200,甚至收到通知在客户端应用程序时,打这个网址从邮递员。但是,当我试图做同样的Android使用retrofit,我得到的状态400坏请求。

安卓代码如下

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对象的日志输出

DTAG:NotificationBody(registration_ids=[cxd-PHM-QOyLcnLcPozjKA.APA91bGIG-NDg-hSYMlTGWm-ZVaM0hR7Om77CaksvZ4bLDKM0gU_xYk9_Um1aOzPEU: APA91bGIG-NDg-hSYMlTGWm-ZVaM0hR7Om77CaksvZ4bLDKM0gU_xYk9_Um1aOzPExGR40FeHAqQpkjt_7-HiG8SMPtF5HLrUjCrcD4Asq_ZcEv-Du5AcMthcYjaZjisduLkBPhgPH0b]。优先级=较高,通知=通知(title=神州公学,body=你好))

android firebase firebase-cloud-messaging retrofit retrofit2
1个回答
0
投票

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

你需要把这个对象发布到上面的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 >"
}

编码

这只是一个暂时的解决方案 。还在寻找解决办法。

© www.soinside.com 2019 - 2024. All rights reserved.