Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PWA - beforeinstallprompt not called

Hello I'm trying to install a custom PWA "Add to Homescreen".

The ServiceWorkerRegistration is successful.

But the function beforeinstallpromp is not calling after register.

<script type="text/javascript">    function request_debug(paramdata){      document.getElementById('output').innerHTML += '<BR>'+ paramdata;    }    window.addEventListener('load', function() {        document.getElementById('output').style.display = "block";         if('serviceWorker' in navigator) {        navigator.serviceWorker.register('sw.js').then(function(registration) {         console.log('Service worker  registrado com sucesso:', registration);         request_debug(registration);        }).catch(function(error) {         console.log('Falha ao Registrar o Service Worker:', error);         request_debug(error);        });            var isTooSoon = true;           window.addEventListener('beforeinstallprompt', function(e) {                //e.preventDefault();               //e.prompt();               //promptEvent = e;               request_debug(' window.addEventListener beforeinstallprompt fired!')                if (isTooSoon) {                 //e.preventDefault(); // Prevents prompt display                 // Prompt later instead:                 setTimeout(function() {                   isTooSoon = false;                   e.prompt(); // Throws if called more than once or default not prevented                 }, 4000);               }            });      }else{        console.log('serviceWorker not in navigator');       request_debug('serviceWorker not in navigator');      }     });  </script> 

Also my service worker in root directory... HTTPS is secure!

my manifest:

{   "background_color": "purple",   "description": "lojaportaldotricot TESTE",   "display": "standalone",   "icons": [     {       "src": "/componentes/serviceWorker/fox-icon.png",       "sizes": "192x192",       "type": "image/png"     }   ],   "name": "lojaportaldotricot",   "short_name": "lojaportaldotricot",   "start_url": "/dashboard" } 

It's only workes when I set "Enable" chrome://flags/#bypass-app-banner-engagement-checks


Edit: Look's like I've found the problem. The Audits tabs of Chrome's DevTools(F12) gives debugging information. enter image description here

like image 781
sealabr Avatar asked Jun 08 '18 14:06

sealabr


People also ask

What is Beforeinstallprompt?

The beforeinstallprompt event fires on devices when a user is about to be prompted to "install" a web application. It may be saved for later and used to prompt the user at a more suitable time.

How do I know if PWA is installed?

Check if your PWA is installed #getInstalledRelatedApps() from within the scope of your PWA to check if it is installed. If getInstalledRelatedApps() is called outside the scope of your PWA, it will return false.

What is a PWA install event?

For sites that pass the PWA install criteria, the browser triggers an event to prompt the user to install it. The good news is that you can use this event to customize your prompt and invite users to install your app. Users may not be familiar with the PWA install process.

How do I install a PWA on my Device?

Edge, Chrome and Samsung Internet offer a native installation prompt. This is done by adding an event handler to the beforeInstallPrompt event. Other browsers, including iPhone Safari, still require a manual prompt. Users can still install or add a PWA to the homescreen through existing channels.

How does the beforeinstallprompt event work?

The Chromium based browsers trigger the beforeinstallprompt event in the background. You must register an event callback to handle the event triggering. In fact, the prompt is gated behind a user action. This is a technical way of saying the user must click a button before you can display the native installation prompt.

Which browsers require a manual prompt to install a PWA?

Other browsers, including iPhone Safari, still require a manual prompt. Users can still install or add a PWA to the homescreen through existing channels. There are still many variances with the overall PWA engagement process between browsers and platforms.


2 Answers

Try this :

 <script>     let deferredPrompt;      window.addEventListener('beforeinstallprompt', function(event) {       // Prevent Chrome 67 and earlier from automatically showing the prompt       e.preventDefault();       // Stash the event so it can be triggered later.       deferredPrompt = e;     });      // Installation must be done by a user gesture! Here, the button click     btnAdd.addEventListener('click', (e) => {       // hide our user interface that shows our A2HS button       btnAdd.style.display = 'none';       // Show the prompt       deferredPrompt.prompt();       // Wait for the user to respond to the prompt       deferredPrompt.userChoice         .then((choiceResult) => {           if (choiceResult.outcome === 'accepted') {             console.log('User accepted the A2HS prompt');           } else {             console.log('User dismissed the A2HS prompt');           }           deferredPrompt = null;         });     });      </script> 

beforeinstallprompt will only be fired when some conditions are true :

  • The PWA must not already be installed
  • Meets a user engagement heuristic (previously, the user had to interact with the domain for at least 30 seconds, this is not a requirement anymore).
  • Your web app must include a web app manifest.
  • Your web app must be served over a secure HTTPS connection.
  • Has registered a service worker with a fetch event handler.
like image 177
Sreelal TS Avatar answered Sep 18 '22 11:09

Sreelal TS


Along with all of those steps above, also check that the app is uninstalled here: chrome://apps

Just deleting the app from the Chrome Apps folder on your Mac does not seem to remove it from Chrome

If the app was previously installed, the beforeinstallprompt will not be triggered, and no errors will be thrown either :(

like image 27
andimiya Avatar answered Sep 21 '22 11:09

andimiya