【发布时间】:2018-09-07 14:56:30
【问题描述】:
我正在与服务人员合作以在我的用户之间显示通知。在我的代码中,我包含了notificationclick 事件。通过这个事件,我试图管理两个案例。第一种情况,如果在我的浏览器中打开了我网站的页面,请不要打开它,而是专注于它。第二种情况,如果我的浏览器没有显示我的网站,请打开它并专注于它。但是我没有成功...
这是我当前的代码:
self.addEventListener('notificationclick', function (e) {
console.log('notification was clicked')
var notification = e.notification;
var action = e.action;
if (action === 'close') {
notification.close();
} else {
// This looks to see if the current is already open and
// focuses if it is
e.waitUntil(
self.clients.matchAll().then(function(clientList) {
console.log(clientList)
if (clientList.length > 0) {
console.log(clientList[0])
return clientList[0].focus();
}
return self.clients.openWindow('/');
})
);
};
});
【问题讨论】:
标签: javascript push-notification service-worker workbox