简体   繁体   English

PWA:如何在安装提示之前重新触发?

[英]PWA: How to retrigger beforeinstallprompt?

Our site uses PWA so that the visitor can choose to Add to Home Screen (A2HS).我们的网站使用 PWA,以便访问者可以选择添加到主屏幕 (A2HS)。 However, from Google Analytics data, the Dismiss rate is too high compared to Acceptance rate.但是,从 Google Analytics 数据来看,与接受率相比,拒绝率太高了。

We plan to make the UX more intuitive and clearer to improve the acceptance rate.我们计划让用户体验更直观、更清晰,以提高接受率。 However, we also want to revive those visitors already dismissed the A2HS dialog.但是,我们也希望恢复那些已经被 A2HS 驳回的访问者的对话。

How to do so?怎么做? To the extend of my knowledge, we only can add beforeinstallprompt listener but there is no openinstallprompt method.据我所知,我们只能添加beforeinstallprompt侦听器,但没有openinstallprompt方法。

For security reasons, as others have written as well, browsers don't allow you to manually trigger the install event.出于安全原因,正如其他人所写的那样,浏览器不允许您手动触发安装事件。

However, there is a way you can test it yourself.但是,有一种方法可以让您自己测试。 Go to chrome://flags and enable "Bypass user engagement checks"转到 chrome://flags 并启用“绕过用户参与度检查”

This will kick off the prompt so you can test.这将启动提示,以便您进行测试。

Cheers干杯

No, You can't trigger the install banner and its driven by the browsers.不,您不能触发安装横幅及其由浏览器驱动。 If your site meets all PWA criteria and if the user is visiting frequent enough(how frequent enough is not explicitly stated by browser vendors), browsers will show the prompt again.如果您的站点满足所有 PWA 标准并且用户访问的频率足够高(浏览器供应商没有明确说明足够频繁),浏览器将再次显示提示。 We can't trigger the same with our code.我们不能用我们的代码触发同样的事情。 Refer this answer on why it behaves that way and whats the alternate solution.请参阅此答案,了解它为什么会这样做以及替代解决方案是什么。

Anand's answer is correct for now.阿南德的回答目前是正确的。 But starting Chrome 68, Chrome will not automatically show the A2HS prompt.但是从 Chrome 68 开始,Chrome 不会自动显示 A2HS 提示。 You will need to explicitly tell Chrome to trigger the prompt.您需要明确告诉 Chrome 触发提示。

在此处输入图片说明

According to Google's documentation, here is the snippet of code to handle the prompt;根据谷歌的文档,这里是处理提示的代码片段;

Listen for the beforeinstallpromptbeforeinstallprompt

let deferredPrompt;

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

Insert the following code in a listener that will trigger the prompt在将触发提示的侦听器中插入以下代码

// 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;
});

Refer this link for further information.请参阅此链接以获取更多信息。

In Dev mode ,在开发模式下

Try this in devtools(tried in chrome) console to trigger the event:在 devtools(在 chrome 中试过)控制台中试试这个来触发事件:

event = new Event('beforeinstallprompt')
window.dispatchEvent(event)

Caution: We won't be able to open the in-browser modal by calling prompt on the event.注意:我们将无法通过在事件上调用prompt来打开浏览器内模式。

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

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