0

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?

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.

send post request to firebase cloud messaging by ajax?

{
  "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)
            }
        });`
Daniel Jackson
  • 1,036
  • 1
  • 11
  • 35
  • FYI You can indent code blocks by using the {} button in the editor. – Doug Stevenson Jan 17 '19 at 18:21
  • I always try but every time I do the page errors out and I can't post. If I delete every single character on the page I'm still shown that there's an error and I have to discard and start over. But thanks for the edit. – Daniel Jackson Jan 17 '19 at 18:23
  • What does "errors out" mean? People format text as code all the time. – Doug Stevenson Jan 17 '19 at 18:28
  • Correct me if I'm wrong, but doesn't `JSON.stringify` return a *String* value? The request requires an actual JSON payload. Not a String in JSON format (not sure if just by setting the contentType handles this immediately) Could you try [sending the request using Postman](https://stackoverflow.com/a/45310143/4625829) and see if it still returns the same error? Also a link with to the StackOverflow post you mentioned could be helpful. – AL. Jan 17 '19 at 19:57
  • @DougStevenson if you use the brace button and there's an error with the spacing then you get a red outline around the box, a red exclamation, and a text description. It doesn't go away even after deleting all text in the entire box. – Daniel Jackson Jan 17 '19 at 20:04
  • And the text description of the error is telling you what? – Doug Stevenson Jan 17 '19 at 20:08
  • @AL. I updated the post to include a link of where the suggestion was to use `JSON.stringify` stating that it works. – Daniel Jackson Jan 17 '19 at 20:10
  • @DougStevenson That there's an error. I didn't record to memory what the message said. It was about spacing or indenting. I'd try doing as it said to fix but nothing I did would remove the error and properly render. Even removing all text. So I went with the only other way I know of. – Daniel Jackson Jan 17 '19 at 20:12
  • It was probably telling you what you did wrong. Just select the text you want to indent, making sure the first line of the code is at column 0, then press the button. It always works for me. – Doug Stevenson Jan 17 '19 at 20:16
  • I tried fixing the spacing and indenting like suggested. I will try again next time. I thought something deeper was wrong and what threw me off was removing all text and still was an error. But I will try again. – Daniel Jackson Jan 17 '19 at 20:17

1 Answers1

-1

You are sending the legacy JSON to the v1 endpoint.

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

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

Michael Fever
  • 845
  • 2
  • 8
  • 26