简体   繁体   English

PMA Service Worker 通知点击不起作用

[英]PMA Service worker notification click isn't working

I am trying to show a notification and do something when it is clicked.我正在尝试显示通知并在单击时执行某些操作。 The part that shows is working well, is receiving data from the server, however, the function of clicking on the notification does not work, I have done everything the documentation says, what I found on the web and here, everything points with the same functionality that I have implemented but it doesn't seem to work.显示的部分运行良好,正在从服务器接收数据,但是,单击通知的功能不起作用,我已经完成了文档说明的所有内容,我在网上和这里找到的内容,所有内容都相同我已经实现但它似乎不起作用的功能。 I have made fictitious practices and as long as the data is not loaded from the server, the click is triggered, otherwise, with the data it does not.我做了虚构的做法,只要数据不是从服务器加载的,就会触发点击,否则,数据不会。

Can anyone tell me what I am doing wrong?谁能告诉我我做错了什么? I'll really apreciate some help, I've two days with this.我真的很感激一些帮助,我有两天的时间。

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

I've also tried with notificationclose to see if it catchs the click but it does not work either.我也尝试过使用 notificationclose 来查看它是否捕捉到了点击,但它也不起作用。

Important to note that it does not display any error or warning, it does simple do anything.重要的是要注意它不显示任何错误或警告,它可以简单地做任何事情。 Found the solution!找到了解决办法! See First Answer.请参阅第一个答案。

I found the solution!我找到了解决方案! After playing around with the code and reading some others documents, it turns out that if my service on my server is a threading type, in my js has to be waiting for an answer.在玩弄代码并阅读其他一些文档后,事实证明,如果我的服务器上的服务是线程类型,则在我的 js 中必须等待答案。 If someone needs it.如果有人需要的话。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM