请解释如何使用 XHR 和 Javascript 进行推送通知。或者是否有任何其他方式可以在渐进式网络应用程序中发送推送通知。我已经创建了 curl 命令,当我在发送的终端推送通知中执行它时,如何在按钮单击时执行它?
这是我的 cURL 命令:-
curl --header "Authorization: key=AIzaSxUdg" --header Content-Type:"application/json" https://android.googleapis.com/gcm/send -d "{\"registration_ids\":[\"cxA-dUj8BTs:APAvGlCYW\"]}"
这是我尝试过的:-
function send()
{
navigator.serviceWorker.ready
.then(function(registration) {
registration.pushManager.getSubscription()
.then(function (subscription) {
curlCommand(subscription);
$.ajax({
url: "https://android.googleapis.com/gcm/send",
headers: {
Authorization: "key=AIzaSxUdg",
},
contentType: "application/json",
data: JSON.stringify({
"registration_ids": [endpoint]
}),
xhrFields: {
withCredentials: true
},
crossDomain: true,
type:"push",
dataType: 'json'
})
.done(function() {
alert('done');
})
.fail(function() {
alert('err');// Error
});
})
})
}
但它显示错误 ----- XMLHttpRequest cannot load https://android.googleapis.com/gcm/send。对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。因此不允许访问源“ http://localhost:8880 ”。