简体   繁体   English

触发 Beforeinstallprompt 事件后添加到主屏幕不提示

[英]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.

相关问题 Chrome for Android - 将网络应用添加到主屏幕 - Chrome for Android - add web app to home screen 使用Angular 4在Chrome中添加到主屏幕按钮 - PWA Add To Home screen button in Chrome with Angular 4 Chrome原生应用安装横幅和添加到主屏幕 - Chrome native app install banner and Add to Home Screen 将他的应用程序添加到“添加到主屏幕”/“快捷方式”部分 - Adding his application to the “add to Home screen”/“Shortcut” section 编辑Google App脚本宣言以添加到主屏幕吗? - Editing Google App Script manifesto to add to home screen? 从移动设备打开已安装的 web 应用程序时出现问题(添加到主屏幕) - Issue when open a installed web app from Mobile (add to home screen) PWA不要添加到首页 - PWA don't add to home 应用程序缓存事件 noupdate 不会在 Safari 浏览器上间歇性地触发。 - Application cache event noupdate is not getting fired intermittently on the Safari browser. HTML5网络应用程序在iOS中添加到主屏幕时不缓存 - HTML5 web app not caching when added to home screen in iOS 在Android中安装时,无法在主屏幕上创建应用程序的快捷方式 - Unable to create application's shortcut on Home Screen upon installation in android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM