服务人员通知不开火

时间:2020-09-21 07:46:34

标签: javascript notifications progressive-web-apps service-worker service-worker-events

我有以下问题:我实现了一个服务工作者,我想通过该工作者发送通知(这部分工作正常)。我为这个服务人员创建了事件监听器。但是,安装事件或onnotificationclick事件的事件侦听器起作用。但是,主动关闭通知时,永远不会触发onnotificationclose事件。 有人可以帮助我,告诉我我的错误在哪里吗? 在下面,您可以找到我的代码的相关部分:

在我的服务工作者文件中

self.onnotificationclick = function(event) {
  console.log('On notification click: ', event.notification.tag);
}

self.onnotificationclose = function(event) {
  console.log('On notification close Service worker: ', event.notification.tag);
}

在我的用于发送通知的js文件中

const notificationTitle = 'Message Title';
const notificationOptions = {
    body: ' Message body.',
    tag: 'notification',
    data: {
      "identifier": "identifier",
    }
};

navigator.serviceWorker.getRegistration().then(function (reg) {
  reg.showNotification(notificationTitle, notificationOptions);
});

0 个答案:

没有答案