0

这是我在渐进式 Web 应用程序中发送推送通知的代码。当我单击按钮 sendRequest() 函数被调用并正确发送通知时,它的工作属性。但是推送事件没有被触发。这段代码有什么问题吗

function sendRequest(){
  navigator.serviceWorker.ready
    .then(function(registration) {
      registration.pushManager.getSubscription()
        .then(function (subscription) {        
          curlCommand(subscription);

          var root = 'http://jsonplaceholder.typicode.com/posts';

          var title="Push Notification";
          $(document).ready(function () {
            $.ajax({
              url: root,  
              beforeSend: function(xhr) { xhr.setRequestHeader("Content-Type","application/json");},      
              type: 'POST',
              contentType: 'application/json',
              crossDomain: true,
              dataType: 'json',
              processData: false,
              data: JSON.stringify(subscription),
              success: function (data) {      
                reg.showNotification(title, {
                  body: 'Hello',
                  icon: 'images/icon.png',
                  tag: 'my-tag'    
                });
                console.log(data);  
              },
              error: function(){
                alert("Cannot get data");
              }
            });
          });
        });
    });
}
4

0 回答 0