Send push notifications thorugh GCM in progessive web apps

六眼飞鱼酱① 提交于 2019-12-12 05:38:42

问题


this is my code to send push notification in progressive web app.It works property when i click on button sendRequest() function is called and notification sent properly. but push event is not fired.is there anything wrong in this code

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");
              }
            });
          });
        });
    });
}

来源:https://stackoverflow.com/questions/36660508/send-push-notifications-thorugh-gcm-in-progessive-web-apps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!