Codexshaper / laravel-pwa

Installable PWA for laravel. Implement PWA in your laravel website within 5 mins. It supports both single site and multi tenancy.
MIT License
15 stars 9 forks source link

How to trigger install popup when click a button? #8

Open maab16 opened 3 years ago

maab16 commented 3 years ago

HTML

Javascript

            <script type="text/javascript">
                var pwaInstallBtn = document.getElementById('pwa-install');
                let isInitiatePwa = false;
                pwaInstallBtn.style.display = 'none';
                window.addEventListener('beforeinstallprompt', (e) => {
                  // Prevent the mini-infobar from appearing on mobile
                  e.preventDefault();
                  window.deferredPrompt = e;
                  pwaInstallBtn.style.display = 'block';
                });

                pwaInstallBtn.addEventListener('click', (e) => {
                  // Show the install prompt
                  const deferredPrompt = window.deferredPrompt;
                  deferredPrompt.prompt();
                  // Wait for the user to respond to the prompt
                  deferredPrompt.userChoice.then((choiceResult) => {
                    if (choiceResult.outcome === 'accepted') {
                      console.log('User accepted the install prompt');
                    } else {
                      console.log('User dismissed the install prompt');
                    }
                  });
                });

                window.addEventListener('appinstalled', (evt) => {
                    pwaInstallBtn.style.display = 'none'
                    window.deferredPrompt = null
                  // Log install to analytics
                  // console.log('INSTALL: Success');
                });
            </script>