Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Close browser window and open PWA once PWA installed

I have Chrome installing my PWA on Android - once it's installed I'd like to automatically close the browser window it was installed from, and open the PWA (so the user doesn't continue in the browser window, thinking they're using the PWA) - is this possible?

like image 518
BruceM Avatar asked Dec 16 '18 07:12

BruceM


People also ask

Can a PWA run in the background?

Using a service worker, a Progressive Web App (PWA) can do work in the background, even when the user isn't using the app. Service workers used to be reserved for native apps, but they are now also available to PWAs, providing a better offline experience.

Can a PWA be used offline?

Each request triggers a fetch event that, in this service worker, searches the cache for a match, if there's a match, responds with cached resource. If there isn't a match, the resource is requested normally. Caching resources allows the app to work offline by avoiding network requests.

How do I save PWA to my home screen in Chrome?

Open the menu next to the URL bar. Depending on whether you're using Chrome or Android you'll see a menu option "Install" or "Install App". This is the "Add to Home screen" option displayed for any site that has the necessary features in place.


2 Answers

I had this problem on Android with Chrome. The change that made the difference is adding "target='_blank'" to the link. It looks like:

window.addEventListener('appinstalled', function(event){
    setTimeout(function(){
        presentToUser("<a href='https://myhostname.com' target='_blank'>Go to App</a>")
    }, 10000)}
});

The ten second timeout is to give Android the time to set up the App on the home page. I had made that adjustment earlier; possibly I can remove it? But setting the target was what made this work.

The App opens over the top of Chrome, obscuring it. So closing the browser is not immediately required but is recommended.

like image 174
Brian Button Avatar answered Sep 21 '22 11:09

Brian Button


i was looking for a similar solution and have not yet found a way to do that. I try to describe my findings so far:


CLOSING THE BROWSER WINDOW:

as described in this answer window.close() can only be called on windows/tabs that the script opened itself. Some possible workarounds are being discussed there.


OPENING THE PWA RIGHT AFTER INSTALLATION:

Google describes in their WebApk Fundamentals Article it as follows:

When a Progressive Web App is installed on Android, it will register a set of intent filters for all URLs within the scope of the app. When a user clicks on a link that is within the scope of the app, the app will be opened, rather than opening within a browser tab.

I was hoping that would work also right after the installation/Adding to homescreen from the still open browser window.

Based on testing with two Android devices it seems as if at the moment the user has to manually open the PWA from the homescreen once for chrome/android to interpret the scope of the web apps manifest.json as intend to open the page in standalone.

This is sad for even iOS seems to handle that different.

Maybe I am overlooking something in the Google Article? I also do not fully understand androids intent API - so maybe there is some way to still achieve that (?)

like image 38
gauguerilla Avatar answered Sep 20 '22 11:09

gauguerilla