1

I am attempting to send a push notification from a simple test webpage. I am using firebase. This is my current code:

function sendPushNotification() {
    $.ajax({        
        type : 'POST',
        url : "https://fcm.googleapis.com/fcm/send",
        headers : {
            Authorization : 'key=' + '<my_server_key>',
            'Content-Type' : 'application/json'
        },
        contentType : 'application/json',
        dataType: 'json',
        data: JSON.stringify({"notification": {"body":"Test"}}),
        success : function(response) {
            console.log(response);
        },
        error : function(xhr, status, error) {
            console.log(xhr);                   
        }
    });
}

I get the error:

[Error] Failed to load resource: the server responded with a status of 400 (HTTP/2.0 400) (send, line 0)

Which I am assuming is a bad request. What is the correct way to do this?

1 Answer 1

2

Per the docs, a 400 error is returned when the JSON in your request can't be parsed. If you check the network tab in your browser, you should be able to see the exact failure reason.

Please note that FCM messages shouldn't be sent from the client. Sending messages requires:

A trusted environment such as Cloud Functions for Firebase or an app server on which to build, target and send messages. (FCM docs: How does it work?)

Sign up to request clarification or add additional context in comments.

2 Comments

Do you know the correct call though? (I know that they shouldn't be send from client, but this is for testing)
Have you checked your network tab to see what the error is? You may want to also check there to see what your request looks like.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.