简体   繁体   English

notificationclick事件服务工作者

[英]notificationclick event service worker


I'm working with service worker to display notification between my users. 我正在与服务人员一起在我的用户之间显示通知。 In my code I include notificationclick event. 在我的代码中,我包含了notificationclick事件。 With this event I'm trying to manage two cases. 通过此事件,我试图处理两种情况。 First case, if in my browser the page of my site is opening, don't open it but focus on it. 第一种情况,如果在浏览器中打开了我的网站页面,请不要打开它,而应专注于它。 Second case, if my browser don't show my site, open it and focus on it. 第二种情况,如果我的浏览器没有显示我的网站,请打开它并关注它。 But I haven't been succed... 但是我还没有成功...

Here is my current code: 这是我当前的代码:

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('/');
            })
       );
   };
});
self.addEventListener("notificationclick", (event) => {
    event.waitUntil(async function () {
        const allClients = await clients.matchAll({
            includeUncontrolled: true
        });
        let chatClient;
        let appUrl = 'xyz';
        for (const client of allClients) {
        //here appUrl is the application url, we are checking it application tab is open
            if(client['url'].indexOf(appUrl) >= 0) 
            {
                client.focus();
                chatClient = client;
                break;
            }
        }
        if (!chatClient) {
            chatClient = await clients.openWindow(appUrl);
        }
    }());
});

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

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