Skip to content Skip to sidebar Skip to footer

How To Bring Chrome To Foreground From A Web App

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 background(minimized) to

Solution 1:

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:

functionopenNotificationWindow(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.

Post a Comment for "How To Bring Chrome To Foreground From A Web App"