XHR と Javascript でプッシュ通知を行う方法を説明してください。または、プログレッシブ Web アプリでプッシュ通知を送信する他の方法はありますか。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」へのアクセスは許可されていません。