0

I am using HTTP POST request to give notification to authors on my web-app that their stories are approved by our admin.

My payload looks like this

var payload = {
  "notification": {
    "title": "Story Approved!",
    "body": "Your story is approved"
  },
  "to" : to
}

This is my request

$.ajax({
  type: 'POST',
  url: 'https://fcm.googleapis.com/fcm/send',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'key='+serverKey
  },
  data: payload,
  success: function(response){
    console.log(response);
  },
});

Whenever I try to send the request it gives me the following error

POST https://fcm.googleapis.com/fcm/send 400 ()

I am kinda new to this and according to Firebase Error Response Codes

Invalid JSON 400 Check that the JSON message is properly formatted and contains valid fields (for instance, making sure the right data type is passed in).

But I cannot figure out where I am wrong. Any help would be appreciated

Akshay Gupta
  • 31
  • 2
  • 10

1 Answers1

1

just convert your payload to json like this:

 data: JSON.stringify(msg)

and your code will be like this:

$.ajax({
  type: 'POST',
  url: 'https://fcm.googleapis.com/fcm/send',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'key='+serverKey
  },
  data: JSON.stringify(msg),
  success: function(response){
    console.log(response);
  },
});
mohsen solhnia
  • 366
  • 3
  • 15
  • I'm still getting Invalid JSON payload. Stating `"payload"` is an unknown name. https://stackoverflow.com/questions/54242019/firebase-cloud-messaging-posting-message-says-invalid-json-payload?noredirect=1#comment95311673_54242019 – Daniel Jackson Jan 18 '19 at 15:29