How to bring chrome to foreground from a web app

前端 未结 1 1988
隐瞒了意图╮
隐瞒了意图╮ 2021-01-28 14:18

Is there any windows apps/google chrome flags/google chrome extension/javascript methods or any other thing that would allow to bring a chrome window from b

相关标签:
1条回答
  • 2021-01-28 14:22

    I solved it by adding this in the service worker file:

       self.addEventListener('push', function (e) {
            if (e.data) {
                var payloadData = e.data.json();
                self.clients.matchAll({includeUncontrolled: true}).then(function (clients) {
                    clients[0].postMessage(payloadData); // you can do foreach but I only needed one client to open one window
                });
            }
        });
    

    And this in the main.js file:

    function openNotificationWindow(url) {
        var myWindow = window.open(url);   // Opens a new window
        myWindow.focus();
        myWindow.location.reload(true);
    }
    
    navigator.serviceWorker.addEventListener('message', function (event) {
        if (typeof event.data.data !== 'undefined') {
            openNotificationWindow(url);
        }
    });
    

    Now everytime I send a notification either with the window in foreground or in the background the push event fires in the serviceworker and then is using postMessage() to comunicate with the some main.js file where you have access to the window DOM component.

    0 讨论(0)
提交回复
热议问题