2

即使在符合所有 PWA 规范并在 Light House 上进行检查后,添加到主屏幕也不会提示。

我试过下面的代码来检查应用程序是否已经安装。但是 appinstalled 事件没有被触发并且 beforeinstallprompt 事件被成功触发。

// 已安装应用

window.addEventListener('appinstalled', (evt) => { app.logEvent('a2hs', 'installed'); });

// 安装前提示

  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);
});`
4

3 回答 3

2

从您的代码中删除此行

event.preventDefault();

从 Chrome 76 开始,preventDefault() 停止了自动迷你信息栏的出现

更多细节在这里
https://developers.google.com/web/fundamentals/app-install-banners/

于 2019-09-25T10:07:26.960 回答
0

通过删除,event.preventDefault()您将无法再控制该事件!

我建议以精简的方式来控制事件,并获取有关安装的信息,请尝试以下代码:

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
        });
 })
于 2020-10-17T22:03:23.650 回答
0
 event.preventDefault(); 

这导致了你的问题。去掉它。

于 2020-05-17T20:31:59.310 回答