简体   繁体   English

PWA 添加到主屏幕不显示弹出窗口

[英]PWA add to home screen not showing popup

I implemented PWA for my static site.我为我的 static 网站实施了 PWA。 service worker register successfully and my page is also working in offline as I expected but the real problem is in add to Home screen service worker 注册成功,我的页面也像我预期的那样离线工作,但真正的问题是添加到主屏幕

I included manifest.json with appropriate icons and still, I don't see the add home screen pop up and while trying to push the add to home screen from inspected only i able to see the popup我包含了带有适当图标的manifest.json ,但我仍然没有看到弹出的添加主屏幕,并且在尝试将添加推送到主屏幕时,我只能看到弹出窗口

This my site URL: https://www.savesoftinc.com/这是我的网站 URL: https://www.savesoftinc.com/

Manifest JSON:清单 JSON:

{
 "name": "Save Soft",
 "short_name": "SaveSoft",
  "start_url": ".",
  "display": "standalone",
  "background_color": "#fff",
  "theme_color": "#0EC3F7",
  "description": "IT Services & Solutions.",
 "icons": [
  {
   "src": "\/android-icon-36x36.png",
   "sizes": "36x36",
   "type": "image\/png",
   "density": "0.75"
  },
  {
   "src": "\/android-icon-48x48.png",
   "sizes": "48x48",
   "type": "image\/png",
   "density": "1.0"
  },
  {
   "src": "\/android-icon-72x72.png",
   "sizes": "72x72",
   "type": "image\/png",
   "density": "1.5"
  },
  {
   "src": "\/android-icon-96x96.png",
   "sizes": "96x96",
   "type": "image\/png",
   "density": "2.0"
  },
  {
   "src": "\/android-icon-144x144.png",
   "sizes": "144x144",
   "type": "image\/png",
   "density": "3.0"
  },
  {
   "src": "\/android-icon-192x192.png",
   "sizes": "192x192",
   "type": "image\/png",
   "density": "4.0"
  },
  {
   "src": "\/ms-icon-512x512.png",
   "sizes": "512x512",
   "type": "image\/png",
   "density": "5.0"
  }
 ]
}

And also try the below code to pop up the banner as suggested by google并尝试使用下面的代码按照谷歌的建议弹出横幅

window.addEventListener('beforeinstallprompt', (e) => {
  // Prevent Chrome 67 and earlier from automatically showing the prompt
  e.preventDefault();
  // Stash the event so it can be triggered later.
  deferredPrompt = e;
  // Update UI notify the user they can add to home screen
  btnAdd.style.display = 'block';
});
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;
    });
});
window.addEventListener('appinstalled', (evt) => {
  app.logEvent('a2hs', 'installed');
});

but it shows error:但它显示错误:

Uncaught ReferenceError: btnAdd is not defined未捕获的 ReferenceError:未定义 btnAdd

ref: https://developers.google.com/web/fundamentals/app-install-banners/参考: https://developers.google.com/web/fundamentals/app-install-banners/

The first step for Add to Home Screen (A2HS) automatic pop-up testing is using the lighthouse audit tools.添加到主屏幕 (A2HS) 自动弹出测试的第一步是使用灯塔审计工具。
You need to run the PWA audit and correct warnings there until you see您需要在那里运行 PWA 审计并纠正警告,直到您看到
--- "User can be prompted to install the Web App" ---“可以提示用户安装Web App”

The lighthouse audit tools are available as a tab in the Chrome dev tools or as a Chrome extension.灯塔审计工具可作为 Chrome 开发工具中的选项卡或作为 Chrome 扩展程序使用。
The extension usually has the more current features.扩展通常具有更新的功能。

Once you see that message you can test the automatic pop-up message on Chrome Desktop and Android (Chrome & Edge)看到该消息后,您可以在 Chrome 桌面和 Android(Chrome 和 Edge)上测试自动弹出消息

Important Things To Note需要注意的重要事项

After you see it the first time, to see the pop-up again you will probably need to totally clear out your browser cache and for第一次看到它后,要再次看到弹出窗口,您可能需要完全清除浏览器缓存,以便
Edge - delete the shortcut Edge - 删除快捷方式
Chrome Desktop - uninstall the app here: chrome://apps/ Chrome 桌面 - 在此处卸载应用程序:chrome://apps/
Crome Android - uninstall the WebApk in your applications Crome Android - 卸载应用程序中的 WebApk

Once you know the automatic A2HS popup works you can then (if desired) intercept the automatic pop-up to make it less annoying to your users.一旦您知道自动 A2HS 弹出窗口的工作原理,您就可以(如果需要)拦截自动弹出窗口以减少对用户的烦扰。 Show them a button to let them know they can A2HS when convenient for them.向他们展示一个按钮,让他们知道他们可以在方便时使用 A2HS。
As described here如此处所述
https://developers.google.com/web/fundamentals/app-install-banners/ https://developers.google.com/web/fundamentals/app-install-banners/

Here is my tester for A2HS .这是我的 A2HS 测试仪
You will see the button show instead of the automatic pop-up.您将看到按钮显示而不是自动弹出窗口。

All the guidelines are provided at : https://developers.google.com/web/fundamentals/app-install-banners/所有指南均在以下网址提供: https : //developers.google.com/web/fundamentals/app-install-banners/

Below are the points for PWA 'Add To Home Screen' banner以下是 PWA“添加到主屏幕”横幅的要点

  1. The web app is NOT already installed.网络应用程序尚未安装。
  2. Add service worker and manifest files.添加服务工作者和清单文件。 service worker should be in a directory where start_url is in its scope. service worker 应该位于 start_url 范围内的目录中。

(eg '/public/home' is in scope of '/' or '/public/') (例如“/public/home”在“/”或“/public/”的范围内)

  1. In manifest.json, prefer_related_applications is NOT set as true在 manifest.json 中,prefer_related_applications 未设置为 true

  2. manifest.json includes: manifest.json 包括:

    • short_name or name, short_name 或名称,
    • icons must include a 192px and a 512px sized icons,图标必须包含一个 192px 和一个 512px 大小的图标,
    • start_url,起始网址,
    • display must be one of: fullscreen, standalone, or minimal-ui显示必须是以下之一:全屏、独立或最小用户界面
  3. Served over HTTPS (required for service workers)通过 HTTPS 提供服务(服务工作者需要)

  4. Has registered a service worker with a fetch event handler.已使用 fetch 事件处理程序注册了 Service Worker。 eg例如

    self.addEventListener('fetch', function(event) {})

  5. Currently, the user has interacted with the domain for at least 30 seconds目前,用户已与域交互至少 30 秒

  6. Code is placed to load service worker in root JS file.代码被放置在根 JS 文件中加载 service worker。

function registerServiceWorker() {
  if ('serviceWorker' in navigator) {
     navigator.serviceWorker.register('/serviceWorker.js') //
        .then(function(reg){
            console.log("service worker registered")
        }).catch(function(err) {
            console.log(err)
        });
  }
  else {
    console.log("Could not find serviceWorker in navigator")
  }
}
registerServiceWorker()
  1. In html page manifest file is added在 html 页面清单文件中添加

    <link rel="manifest" href="/pwa/manifest.json">

  2. Banner was not dismissed earlier(Will not appear for 3 months as per guide for mini-info-bar ).横幅没有更早被解雇(根据迷你信息栏的指南,3 个月内不会出现)。 You can bring it back by clearing cookies.您可以通过清除 cookie 将其恢复。

  3. For iOS devices, need to provide icons set in html page as per Safari Web Content Guide :.对于 iOS 设备,需要根据Safari Web 内容指南提供在 html 页面中设置的图标:。 Presently 'Add to home screen' is only supported for Safari browser.目前仅 Safari 浏览器支持“添加到主屏幕”。 You find this by clicking on share icon.您可以通过单击共享图标找到它。

Is there any way to open Already installed PWA from desktop.有什么方法可以从桌面打开已经安装的 PWA。

Try below code..试试下面的代码..

let btnAdd = document.querySelector(".btnAdd"); // .btnAdd maybe id or class

which is applied on custom prompt button应用于自定义提示按钮

btnAdd.style.display = "block";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM