Faisal Shaikh Faisal Shaikh - 22 days ago 13
jQuery Question

Send Firebase push notification from JavaScript/jQuery

I am trying to send Firebase push notification from my html page to Android app. I am refering this SO answer to implement it. Here is my code:

function post() {
$.ajax({
type : 'POST',
url : "https://fcm.googleapis.com/fcm/send",
headers : {
Authorization : 'key=' + 'xxxxxxxxxxxx-xxx-xxxxxxxxxxxxxx'
},
contentType : 'application/json',
data : {
"to": "videos",
"data": {
"message": "This is push for video!"
}
},
success : function(response) {
console.log(response);
},
error : function(xhr, status, error) {
console.log(xhr.error);
}
});


Currently, I am facing issue that in response of
POST request
I am getting following error:

JSON_PARSING_ERROR: Unexpected character (t) at position 0.

Answer

Try to call

JSON.stringify({"to": "videos", "data": {"message": "This is push for video!"}})

or add option dataType: 'json'

Comments