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...
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
On Chrome, if you already have added the site to home screen, beforeinstallprompt is not going to fire. Make sure your site isn't already added to home screen, and clear Chrome's site data, and try again (ideally on a different Android phone as well).
Otherwise, this is not an issue with the manifest spec. It is an issue with the Chrome browser, and you should file a bug at crbug.com/new.
solved! thanks!
@sealabr hELLO
i'M HAVING SAME ISSUE PLEASE HOW DID YOU SOULVE IT
THANKS
@33sKamal check this
https://stackoverflow.com/questions/50762626/pwa-beforeinstallprompt-not-called
Most helpful comment
On Chrome, if you already have added the site to home screen, beforeinstallprompt is not going to fire. Make sure your site isn't already added to home screen, and clear Chrome's site data, and try again (ideally on a different Android phone as well).
Otherwise, this is not an issue with the manifest spec. It is an issue with the Chrome browser, and you should file a bug at crbug.com/new.