javascript - PWA - 当我需要时提示输入 "add to home screen"?

标签 javascript progressive-web-apps

我有一个 PWA 应用,可以正确提示用户将该应用添加到主屏幕。问题是,恕我直言,这种自动行为是很糟糕的用户体验,因为 90% 的情况下,提示会被认为是一个烦人的弹出窗口,而用户只是将其关闭。

我认为更好的用户体验是让用户寻找一种“安装应用程序”的方法,就像在任何页面的页脚中,您通常会看到“在 Android 上下载”或“在App Store”,或者在一些特定的交互之后,当我感觉用户充分参与我的应用程序并且可能有兴趣将其保存到主屏幕时,会出现一些由我控制的弹出窗口。

我已经进行了一些搜索,但无法找到任何方法在需要时触发“添加到主屏幕”弹出窗口。我错过了什么?如何第二次或第三次提示“添加到主屏幕”?

正在寻找 Android 和 iPhone 上的任何解决方案。

最佳答案

来自Provide a custom install experience :

To indicate your Progressive Web App is installable, and to provide a custom in-app install flow:

  1. Listen for the beforeinstallprompt event.
  2. Save the beforeinstallprompt event, so it can be used to trigger the install flow later.
  3. Alert the user that your PWA is installable, and provide a button or other element to start the in-app installation flow.
let deferredPrompt;

window.addEventListener('beforeinstallprompt', (e) => {
  // Prevent the mini-infobar from appearing on mobile
  e.preventDefault();
  // Stash the event so it can be triggered later.
  deferredPrompt = e;
  // Update UI notify the user they can install the PWA
  showInstallPromotion();
});

然后标记安装选项并处理用户事件以返回延迟提示

buttonInstall.addEventListener('click', (e) => {
  // Hide the app provided install promotion
  hideMyInstallPromotion();
  // Show the install 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 install prompt');
    } else {
      console.log('User dismissed the install prompt');
    }
  })
});

关于javascript - PWA - 当我需要时提示输入 "add to home screen"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61254435/

相关文章:

Javascript perf 清理数组

javascript - AngularFire - 如何使用 ng-repeat 定义子模板的项目 ID?

javascript - javascript的setInterval函数

javascript - 即使浏览器关闭,服务人员也会在后台运行吗?

progressive-web-apps - 设置 PWA 的窗口标题和图标

ios - 克服 iOS PWA 缓存 50 MiB 的限制

javascript - event.charCode 在 Firefox 中不起作用

javascript - 使用angularjs ng Repeat和Routes时如何处理内存泄漏?

javascript - IMAP(邮件客户端)的 PWA

android - 在 Chrome Android 中全屏运行的 PWA 不支持显示模式 : standalone