Web服务工作者通知单击 - 在同一页面上打开URL

时间:2017-07-07 14:56:19

标签: javascript html5 web-worker

我有一个网络工作者,我添加了'notificationclick'事件。 我想在同一个标​​签页中导航到网址,而不是打开新标签页。

任何线索如何做到这一点?

self.addEventListener('notificationclick', function (event) {
    const id = event.notification.data.id;
    let path = 'http://' + event.target.location.host;
    if (event.notification.data.type === 'invite') 
        path += '/invite/' + id;
    else 
        path += '/notification/' + id;
    event.notification.close();
    const promiseChain = clients.matchAll({
        type: 'window',
        includeUncontrolled: true
    })
        .then((windowClients) => {
            let matchingClient = null;
            for (let i = 0; i < windowClients.length; i++) {
                const windowClient = windowClients[i];
                if (windowClient.url === path) {
                    matchingClient = windowClient;
                    break;
                }
            }

            if (matchingClient) {
                return matchingClient.focus();
            } else {
                return clients.openWindow(path);
            }
        });

    event.waitUntil(promiseChain);
});

0 个答案:

没有答案