【发布时间】: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