[英]Add to Home Screen not prompt after Beforeinstallprompt event fired
Add to home screen not prompt even after its meets all PWA specification and checked by on Light House.即使在符合所有 PWA 规范并在 Light House 上进行检查后,添加到主屏幕也不会提示。
I have tried below code to check whether app already installed or not.我试过下面的代码来检查应用程序是否已经安装。 but appinstalled Event not getting triggered and beforeinstallprompt Event gets fired successfully.但是 appinstalled 事件没有被触发并且 beforeinstallprompt 事件被成功触发。
// appinstalled // 已安装应用
window.addEventListener('appinstalled', (evt) => { app.logEvent('a2hs', 'installed'); });
// beforeinstallprompt // 安装前提示
window.addEventListener('beforeinstallprompt', (event) => {
event.preventDefault();
deferredPrompt = event;
});```
// manifest.json
`{
"name": "demo",
"short_name": "demo",
"icons": [{
"src": "/static/public/icon/icon-192x192.png",
"sizes": "512x512",
"type": "image/png"
},
{
"src": "/static/public/icon/icon-512x512.png",
"sizes": "192x192",
"type": "image/png"
}
],
"start_url": "/",
"orientation": "portrait",
"display": "standalone",
"theme_color": "#085689",
"background_color": "#085689",
"gcm_sender_id": "103xx3xxx50x",
"gcm_user_visible_only": true
}
`
// service worker
`self.addEventListener('fetch', (event) => {
console.log('event', event);
});`
Remove this line from your code从您的代码中删除此行
event.preventDefault();
Starting with Chrome 76, preventDefault() stopped the automatic mini-infobar from appearing从 Chrome 76 开始,preventDefault() 停止了自动迷你信息栏的出现
More details here更多细节在这里
https://developers.google.com/web/fundamentals/app-install-banners/ https://developers.google.com/web/fundamentals/app-install-banners/
event.preventDefault();
This is causing you the problem.这导致了你的问题。 Remove it.去掉它。
By removing the event.preventDefault()
you have no longer control over the event !通过删除event.preventDefault()
您不再可以控制事件!
I suggest a lean manner to control the event, and get the information about installation, try the code below:我建议以精益的方式来控制事件,并获取有关安装的信息,请尝试以下代码:
window.addEventListener('beforeinstallprompt', (event) => {
event.preventDefault();
deferredPrompt = event;
deferredPrompt.prompt();
deferredPrompt.userChoice.then(result => {
if(result.outcome === "accepted") {
// TODO whatever you want to do when the user accept to install the app
} else {
// TODO whatever you want to do when the user refuse to install the app
});
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.