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.
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.
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.
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.
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.
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.
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.
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 :
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 :(
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With