当前情况
我们发送POST请求Firebase legacy URL https://fcm.googleapis.com/fcm/send
,以将通知JSON有效负载传递到我们的Android应用程序。我们的通知有效负载是标准数据中的嵌套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"
}
}
}
请求标头如下:
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应用中,以创建通知。
问题
我们要迁移到Firebase v1 HTTP request APIs。我正在尝试使用OAuth2 Tokens进行身份验证并将通知传递到Android设备。我想通过V1 API发送的有效负载如下所示:
{
"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"
}
}
}
}
当我们向v1 HTTP API发出HTTP POST请求时,收到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."
}
]
}
]
}
}
但是当数据有效载荷没有如下嵌套时
"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",
}
然后我得到200 OK响应。响应正文如下所示:
{
"name": "projects/<project_id>/messages/<message_id>"
}
期望的行为
我希望能够使用Firebase v1 HTTP API向我们的Android应用程序传递嵌套的data
负载,以便可以呈现通知。发送嵌套的有效载荷时遇到错误,如何实现?这是我用来发出HTTP请求的程序。该程序使用Google-API客户端,FreeMarker,Firebase-Admin和OKHttp。任何帮助将不胜感激。
public class FCMMessaging {
public static void main(String[] args) throws IOException, FirebaseMessagingException {
// File was downloaded from Firebase console URL: https://console.firebase.google.com/project/<project-id>/settings/serviceaccounts/adminsdk
try(InputStream is =
FCMMessaging.class.getClassLoader().getResourceAsStream("firebase-admin-service-account.json");) {
GoogleCredential googleCredential = GoogleCredential
.fromStream(is)
.createScoped(Collections.singletonList("https://www.googleapis.com/auth/firebase.messaging"));
googleCredential.refreshToken();
String bearerToken = googleCredential.getAccessToken();
Configuration cfg = new Configuration();
// Where do we load the notification payload templates from:
cfg.setClassForTemplateLoading(FCMMessaging.class, "");
// Some other recommended settings:
cfg.setDefaultEncoding("UTF-8");
cfg.setLocale(Locale.US);
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.DEBUG_HANDLER);
Map<String, Object> templateMap = new HashMap<>();
templateMap.put("token",
"my-token-id");
Template template = cfg.getTemplate("notification_payload.json");
StringWriter stringWriter = new StringWriter();
template.process(templateMap, stringWriter);
RequestBody requestBody = RequestBody.create(stringWriter.toString(), MediaType.get("application/json; charset=utf-8"));
Request request = new Request.Builder().url("https://fcm.googleapis.com/v1/projects/<project-id>/messages:send")
.addHeader("Authorization", "Bearer " + bearerToken).post(requestBody).build();
OkHttpClient client = new OkHttpClient();
try (Response response = client.newCall(request).execute()) {
System.out.println(response.body().string());
}
} catch (TemplateException te) {
te.printStackTrace();
}
}
}
答案 0 :(得分:-1)
我随同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"}