I have a mobile app that needs to receive notifications via POST using OneSignal API. I was testing it on Postman and everything went just fine. Now what i want to do is: Grab a javaScript snippet and consume the OneSignal API to send my mobile any push notification. Now i am trying to do it using an example on w3Schools, but it doesn't seem to work. This is how i was trying to do it:
<button type="button" onclick="loadDoc()">Request data</button>
<p id="demo"></p>
<script>
function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML = this.responseText;
}
};
xhttp.open("POST", "https://onesignal.com/api/v1/notification", true);
xhttp.setRequestHeader("Content-type", "application/json");
xhttp.setRequestHeader("Authorization", "Basic xxxxxxxxxxxxxxxxxx");
xhttp.send({
"app_id" : "xxxxxxxxxxxxxxxxxxxxxxxxxx",
"contents": {"en": "Hello World!"} ,
"included_segments" : ["All"]
});
}
</script>
</body>
</html>
I can't really understand how am i supposed to do it.