Android Firebase v1 HTTP api不支持嵌套的JSON对象
当前情况 我们发送了一个POST请求Android Firebase v1 HTTP api不支持嵌套的JSON对象,android,firebase,firebase-cloud-messaging,android-notifications,Android,Firebase,Firebase Cloud Messaging,Android Notifications,当前情况 我们发送了一个POST请求https://fcm.googleapis.com/fcm/send向我们的Android应用程序提供通知JSON负载。我们的通知负载是标准数据包中的嵌套JSON对象。我们的邮递员有效载荷样本如下所示 { "registration_ids": [ "${registration_id}"], "priority":"HIGH", "data": { "notification": { "title": "Notificat
https://fcm.googleapis.com/fcm/send
向我们的Android应用程序提供通知JSON负载。我们的通知负载是标准数据包中的嵌套JSON对象。我们的邮递员有效载荷样本如下所示
{
"registration_ids": [
"${registration_id}"],
"priority":"HIGH",
"data": {
"notification": {
"title": "Notification Data Title From Postman",
"body": "Notification Data Body From Postman",
"imageurl": "https://d2x51gyc4ptf2q.cloudfront.net/content/uploads/2015/10/GettyImages-51205958-700x367.jpg"
},
"fcm_options": {
"analytics_label": "postman"
}
}
}
{
"message": {
"token": "${token}",
"data": {
"notification": {
"title": "Notification Data Title From v1 API",
"body": "Notification Data Body From v1 API",
"imageurl": "https://d2x51gyc4ptf2q.cloudfront.net/content/uploads/2015/10/GettyImages-51205958-700x367.jpg",
"extra_key": "extra_value"
}
},
"android": {
"priority": "HIGH",
"fcm_options": {
"analytics_label": "Test_Command_line"
}
}
}
}
"data": {
"title": "Title that is not nested",
"body": "Body that is not nested",
"imageurl": "https://timedotcom.files.wordpress.com/2016/08/margot-robbie-beer-shower.jpg",
}
{
"name": "projects/<project_id>/messages/<message_id>"
}
请求标头如下所示:
Content-Type: application/json; charset=utf-8`
Authorization: key={{server_key}}
在本例中,数据对象中的嵌套JSON对象用于成功构建通知,如下所示
"data": {
"notification": {
"title": "Notification Data Title From Postman",
"body": "Notification Data Body From Postman",
"imageurl": "https://d2x51gyc4ptf2q.cloudfront.net/content/uploads/2015/10/GettyImages-51205958-700x367.jpg"
}
}
服务器密钥从Firebase控制台的项目设置中获取。此有效负载已成功交付到适当设备上的Android应用程序,以创建通知
"data": {
"notification": {
"title": "Notification Data Title From Postman",
"body": "Notification Data Body From Postman",
"imageurl": "https://d2x51gyc4ptf2q.cloudfront.net/content/uploads/2015/10/GettyImages-51205958-700x367.jpg"
}
}
问题
我们想迁移到。我正在尝试使用验证并向Android设备发送通知。下面给出了我希望通过v1api发送的有效负载
{
"registration_ids": [
"${registration_id}"],
"priority":"HIGH",
"data": {
"notification": {
"title": "Notification Data Title From Postman",
"body": "Notification Data Body From Postman",
"imageurl": "https://d2x51gyc4ptf2q.cloudfront.net/content/uploads/2015/10/GettyImages-51205958-700x367.jpg"
},
"fcm_options": {
"analytics_label": "postman"
}
}
}
{
"message": {
"token": "${token}",
"data": {
"notification": {
"title": "Notification Data Title From v1 API",
"body": "Notification Data Body From v1 API",
"imageurl": "https://d2x51gyc4ptf2q.cloudfront.net/content/uploads/2015/10/GettyImages-51205958-700x367.jpg",
"extra_key": "extra_value"
}
},
"android": {
"priority": "HIGH",
"fcm_options": {
"analytics_label": "Test_Command_line"
}
}
}
}
"data": {
"title": "Title that is not nested",
"body": "Body that is not nested",
"imageurl": "https://timedotcom.files.wordpress.com/2016/08/margot-robbie-beer-shower.jpg",
}
{
"name": "projects/<project_id>/messages/<message_id>"
}
当我们向v1httpapi发出httppost请求时,我得到一个400错误,错误消息如下
{
"error": {
"code": 400,
"message": "Invalid JSON payload received. Unknown name \"title\" at 'message.data[3].value': Cannot find field.\nInvalid JSON payload received. Unknown name \"body\" at 'message.data[3].value': Cannot find field.\nInvalid JSON payload received. Unknown name \"imageurl\" at 'message.data[3].value': Cannot find field.\nInvalid JSON payload received. Unknown name \"extra_key\" at 'message.data[3].value': Cannot find field.",
"status": "INVALID_ARGUMENT",
"details": [
{
"@type": "type.googleapis.com/google.rpc.BadRequest",
"fieldViolations": [
{
"field": "message.data[3].value",
"description": "Invalid JSON payload received. Unknown name \"title\" at 'message.data[3].value': Cannot find field."
},
{
"field": "message.data[3].value",
"description": "Invalid JSON payload received. Unknown name \"body\" at 'message.data[3].value': Cannot find field."
},
{
"field": "message.data[3].value",
"description": "Invalid JSON payload received. Unknown name \"imageurl\" at 'message.data[3].value': Cannot find field."
},
{
"field": "message.data[3].value",
"description": "Invalid JSON payload received. Unknown name \"extra_key\" at 'message.data[3].value': Cannot find field."
}
]
}
]
}
}
但当数据有效负载未按如下所示嵌套时
{
"registration_ids": [
"${registration_id}"],
"priority":"HIGH",
"data": {
"notification": {
"title": "Notification Data Title From Postman",
"body": "Notification Data Body From Postman",
"imageurl": "https://d2x51gyc4ptf2q.cloudfront.net/content/uploads/2015/10/GettyImages-51205958-700x367.jpg"
},
"fcm_options": {
"analytics_label": "postman"
}
}
}
{
"message": {
"token": "${token}",
"data": {
"notification": {
"title": "Notification Data Title From v1 API",
"body": "Notification Data Body From v1 API",
"imageurl": "https://d2x51gyc4ptf2q.cloudfront.net/content/uploads/2015/10/GettyImages-51205958-700x367.jpg",
"extra_key": "extra_value"
}
},
"android": {
"priority": "HIGH",
"fcm_options": {
"analytics_label": "Test_Command_line"
}
}
}
}
"data": {
"title": "Title that is not nested",
"body": "Body that is not nested",
"imageurl": "https://timedotcom.files.wordpress.com/2016/08/margot-robbie-beer-shower.jpg",
}
{
"name": "projects/<project_id>/messages/<message_id>"
}
然后我得到一个200 OK的回复。下面给出了响应主体
{
"registration_ids": [
"${registration_id}"],
"priority":"HIGH",
"data": {
"notification": {
"title": "Notification Data Title From Postman",
"body": "Notification Data Body From Postman",
"imageurl": "https://d2x51gyc4ptf2q.cloudfront.net/content/uploads/2015/10/GettyImages-51205958-700x367.jpg"
},
"fcm_options": {
"analytics_label": "postman"
}
}
}
{
"message": {
"token": "${token}",
"data": {
"notification": {
"title": "Notification Data Title From v1 API",
"body": "Notification Data Body From v1 API",
"imageurl": "https://d2x51gyc4ptf2q.cloudfront.net/content/uploads/2015/10/GettyImages-51205958-700x367.jpg",
"extra_key": "extra_value"
}
},
"android": {
"priority": "HIGH",
"fcm_options": {
"analytics_label": "Test_Command_line"
}
}
}
}
"data": {
"title": "Title that is not nested",
"body": "Body that is not nested",
"imageurl": "https://timedotcom.files.wordpress.com/2016/08/margot-robbie-beer-shower.jpg",
}
{
"name": "projects/<project_id>/messages/<message_id>"
}
我随官方的Golang图书馆和它的物品发送如下:
{
"data": {
"age":"37",
"elite":"1",
"message":"test 4",
"name":"Денис"},
"notification":{
"title":"У Вас 2 новых сообщения!",
"body":"test 4"},
"android":{
"collapse_key":"1",
"priority":"normal",
"data":{
"badge":"2",
"sound":"1",
"type":"1"}},
"fcm_options":{
"analytics_label":"1"},
"token":"some_token"}
运气好吗?我也面临同样的问题。@derryltomas将JSON负载字符串化,发送是一个键:值对。在Firebase消息处理程序中反序列化。谢谢!同时我也想到了这一点。我注意到设备上接收到的有效负载似乎与旧API完全相同,这使我得出结论,FCM一直在进行内部串接。你也注意到了同样的情况吗?或者,您是否必须调整应用程序代码中的任何内容以响应从HTTP V1 API接收到的新负载?我不必更改Android代码库,但我的后端服务必须更新模板,并序列化JSON对象。