Firebase云消息传递-发布消息说无效的JSON有效负载 [英] Firebase Cloud Messaging - Posting Message Says Invalid JSON Payload

查看:81
本文介绍了Firebase云消息传递-发布消息说无效的JSON有效负载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在使用jQuery Ajax来发布必须从某人的node.js应用程序获取的承载令牌,因为我找不到有关如何使用您的首选语言来获取承载令牌的Google文档.仅用于FCM.我正在使用JSON.stringify准备有人在此处显示的有效负载数据.我尝试过没有,但是我只得到基本错误消息Invalid_Arguments.使用JSON.stringify可以显示更多有关该错误的信息.指出"to"和"notification"属性是未知名称,但它们是Google文档中使用的属性名称.那我在做什么错了?

So I'm using jQuery Ajax to post using a bearer token that I'm having to get from someone's node.js app because I can't find Google docs on how to use your preferred language to get a bearer token for FCM use only. I'm using JSON.stringify to prepare the payload data which someone shows on stackoverflow here. I've tried without but I just get a basic error message of Invalid_Arguments. Using JSON.stringify shows more to the error. It's stating that "to" and "notification" properties are unknown names but those are the property names used in Google docs. So what am I doing wrong?

这是某人说JSON.stringify起作用的链接.在此之前,我尝试不使用JSON.stringify导致Invalid JSON Payload以及我提出这个问题的原因.

Here's the link where someone says JSON.stringify works. Before then I tried without JSON.stringify which led to Invalid JSON Payload and my reason for asking this question.

通过Ajax将发布请求发送到Firebase云消息传递吗?

{
  "error": {
    "code": 400,
    "message": "Invalid JSON payload received. Unknown name \"to\": Cannot find field.\nInvalid JSON payload received. Unknown name \"notification\": Cannot find field.",
    "status": "INVALID_ARGUMENT",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.BadRequest",
        "fieldViolations": [
          {
            "description": "Invalid JSON payload received. Unknown name \"to\": Cannot find field."
          },
          {
            "description": "Invalid JSON payload received. Unknown name \"notification\": Cannot find field."
          }
        ]
      }
    ]
  }
}


$.post({
            method: "POST",
            url: "https://fcm.googleapis.com/v1/projects/floridarecycling-b91ec/messages:send",
            dataType: "json",
            contentType: "application/json",
            headers: {
                'Authorization': 'Bearer ' +
                    'ya29.c.ElqUBiCx...wyKqUqKlrg7yhPw'
            },
            data: JSON.stringify({
                "to": "user_token",
                "notification": {
                    "title": "Test",
                    "body": "test"
                }
            })
            success: function () {
                console.log("Success")
            },
            error: function (err) {
                console.log("error ", err)
            }
        });`

推荐答案

您正在将旧版JSON发送到v1端点.

You are sending the legacy JSON to the v1 endpoint.

如果您使用收件人"字段,则需要发送到Legacy Firebase Cloud Messaging端点,即:

If you are using the "to" field you need to send to the Legacy Firebase Cloud Messaging endpoint which is:

 https://fcm.googleapis.com/fcm/send

如果您想使用新的Firebase Cloud Messaging,请在此处获取有关新格式和参数的文档:

If you want to use the new Firebase Cloud Messaging the documentation for the new format and parameters is here:

https://firebase.google.com/docs /reference/fcm/rest/v1/projects.messages

这篇关于Firebase云消息传递-发布消息说无效的JSON有效负载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆