Can't send Firebase Cloud Messaging with axios

Viewed 1291

Wanted to develop Web notification on Firebase Cloud Messaging. I'm using axios for requesting but it seems not to be working. It occurs an error:

Error: Request failed with status code 400

The Code:

 componentDidMount() {
    const notifications = {
      notification: [
        {
          title: "Hola!",
          body: "Hello World",
          icon: "https://xxxx.com/icon.png",
          click_action: `https://example.com/`
        }
      ],
      to: token
     };
    axios
      .post(
        "https://fcm.googleapis.com/fcm/send",
        {
          notifications
        },
        {
          headers: {
            "Content-Type": "application/json",
            Authorization:
              "key=<FCM_SERVER_KEY>"
          }
        }
      )
      .then(response => {
        console.log("response" + response);
      })
      .catch(error => {
        console.log(error);
      });
1 Answers

You need to set the API KEY on the headers of axios.

The API KEY you found on the Console of Firebase Cloud Messaging.

Related