Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript PMA服务人员通知单击isn';行不通_Javascript_Notifications_Service Worker_Web Push - Fatal编程技术网

Javascript PMA服务人员通知单击isn';行不通

Javascript PMA服务人员通知单击isn';行不通,javascript,notifications,service-worker,web-push,Javascript,Notifications,Service Worker,Web Push,我正在尝试显示通知,并在单击时执行某些操作。显示的部分工作正常,从服务器接收数据,但是,单击通知的功能不起作用,我已经完成了文档中所述的所有操作,我在web和此处找到的内容,所有内容都与我实现的功能相同,但似乎不起作用。我做了一些虚构的实践,只要数据不是从服务器加载的,就会触发点击,否则,数据不会触发点击 谁能告诉我我做错了什么?我真的很感激你的帮助,我有两天的时间来做这个 self.addEventListener('push', (e) => { let notification

我正在尝试显示通知,并在单击时执行某些操作。显示的部分工作正常,从服务器接收数据,但是,单击通知的功能不起作用,我已经完成了文档中所述的所有操作,我在web和此处找到的内容,所有内容都与我实现的功能相同,但似乎不起作用。我做了一些虚构的实践,只要数据不是从服务器加载的,就会触发点击,否则,数据不会触发点击

谁能告诉我我做错了什么?我真的很感激你的帮助,我有两天的时间来做这个

self.addEventListener('push', (e) => {
  let notification = e.data.json();
  const title = 'My app ' + notification.title;
  const options = {
      body: notification.msg,
      actions: [
        { action: 'yes', title: 'Aprobar' },
        { action: 'no', title: 'Rechazar' }
      ]
  };
  self.registration.showNotification(title, options);
}); //to show the notification with server info

self.addEventListener('notificationclick', function (event) {
   console.log(event);
   var noti = event.notification.data;
   let r = event.notification.data.json();
   console.log(noti); }, 
false); //handling the click
我也尝试过notificationclose,看看它是否捕捉到了点击,但它也不起作用


重要的是要注意,它不会显示任何错误或警告,它只执行简单的操作。找到了解决办法!请参阅第一个答案。

我找到了解决方案!在仔细阅读代码和其他文档后,发现如果我的服务器上的服务是线程类型,那么我的js中的服务必须等待答案。如果有人需要它

let notification = '';
self.addEventListener('push', (e) => {
 notification = e.data.json();
 const title = 'My app' + notification.title;
 const options = {
    body: notification.msg,
    actions: [
        { action: 'yes', title: 'Aprobar' },
        { action: 'no', title: 'Rechazar' }
    ]
};
 e.waitUntil(self.registration.showNotification(title, options)); }); //waitUntil to show the notification

self.addEventListener('notificationclick', function (event) {
console.log('[Service Worker] Notification click Received.');
event.notification.close();
let response = event.action;
event.waitUntil(
    fetch(`api/authorizations/processor?uuid=${notification.uuid}&response=${response}&account=${notification.user}`,
        {
            method: 'GET',
            mode: 'cors',
            cache: 'default'
        }).then((result) => {
            if (!result.ok)
                throw result;
            return result.json();
        }).then(function (data) {
            console.log(data);
        }).catch(function (error) {
            console.log(errow);
        })
); }); // waitUntil to expect a action