繁体   English   中英

Chrome:通知显示事件未触发

[英]Chrome: Notification onshow event not triggering

显示通知后,我正在尝试在我的服务人员中运行代码。 我正在使用此 API 显示通知:

self.registration.showNotification(title, options)

我尝试了一些东西:

  1. 我希望返回的 promise 与Notification object 一起完成,然后我可以在它显示后做一些事情。 但是 promise 如果满足undefined

  2. 我试图像这样附加一个 function (当然是在 showNotification 调用之前):

    Notification.onshow = function() { console.log('shown')};

但它没有被触发( https://developer.mozilla.org/en-US/docs/Web/API/Notification/onshow

  1. 我试图做一些类似的事情来为notifictionclick和notificationclose事件附加回调:

    self.addEventListener("notificationshow", (event) => {});

此外,没有工作。

我的主要目标基本上是在显示通知实例后获取它,以便我可以对其进行处理(例如调用notifiaction.close

您可以使用getNotifications (并按标签过滤以获取您的通知)

var options = { tag : 'user_alerts' };

navigator.serviceWorker.ready.then(function(registration) {
  registration.getNotifications(options).then(function(notifications) {
    // do something with your notifications
    notifications[0].close();
  }) 
});

https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/getNotifications

暂无
暂无

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

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