[英]PWA app beforeInstallPrompt not firing in mobile browser but working in normal desktop browser
My case is a bit weird from the rest of the normal PWA installation problems.我的情况与其他正常的 PWA 安装问题相比有点奇怪。 I audited my code using LightHouse and it gave everything green though I am not able to see the option
我使用 LightHouse 审核了我的代码,尽管我看不到该选项,但它使所有内容都变成了绿色
"User Can Be Prompted To Install The Web App". “可以提示用户安装 Web 应用程序”。
I have written some code in React for my custom prompt for PWA apps.我已经在 React 中为 PWA 应用程序的自定义提示编写了一些代码。 and it goes like this
它是这样的
In App.js file in the componentDidMount method在 App.js 文件中的 componentDidMount 方法
componentDidMount(){
window.addEventListener('beforeinstallprompt',e =>{ // For older browsers e.preventDefault(); console.log("Install Prompt fired"); this.installPrompt = e; // See if the app is already installed, in that case, do nothing if((window.matchMedia && window.matchMedia('(display-mode: standalone)').matches) || window.navigator.standalone === true){ return false; } // Set the state variable to make button visible this.setState({ isModalOpen:true }) })
}
componentDidMount(){ window.addEventListener('beforeinstallprompt',e =>{ // For older browsers e.preventDefault(); console.log("Install Prompt fired"); this.installPrompt = e; // See if the app is already installed, in that case, do nothing if((window.matchMedia && window.matchMedia('(display-mode: standalone)').matches) || window.navigator.standalone === true){ return false; } // Set the state variable to make button visible this.setState({ isModalOpen:true }) })
}
componentDidMount(){
window.addEventListener('beforeinstallprompt',e =>{ // For older browsers e.preventDefault(); console.log("Install Prompt fired"); this.installPrompt = e; // See if the app is already installed, in that case, do nothing if((window.matchMedia && window.matchMedia('(display-mode: standalone)').matches) || window.navigator.standalone === true){ return false; } // Set the state variable to make button visible this.setState({ isModalOpen:true }) })
}
With the state isModalOpen I am able to show the custom prompt to the user in normal desktop browser.通过状态isModalOpen,我可以在普通桌面浏览器中向用户显示自定义提示。 But when I run the same thing over mobile browser, this beforeinstallprompt is not getting fired.
但是当我在移动浏览器上运行同样的事情时,这个 beforeinstallprompt 不会被解雇。 I tried in
我试过
Safari Browser in iOS Chrome Browser in iOS iOS 中的 Safari 浏览器 iOS 中的 Chrome 浏览器
Chrome Browser in Android Android 中的 Chrome 浏览器
Can anyone guide me as to what I may be missing.任何人都可以指导我了解我可能缺少什么。 Or if anyone has encountered such issues
或者如果有人遇到过这样的问题
I got the issue resolved.我解决了问题。 The solution here it goes: Created a web.config file in the public folder and added the following lines
这里的解决方案是:在公共文件夹中创建了一个 web.config 文件并添加了以下几行
<?xml version="1.0"?> <configuration> <system.webServer> <staticContent> <mimeMap fileExtension=".json" mimeType="application/json; charset=UTF-8" /> </staticContent> </system.webServer> </configuration>
When I build my application,this web.config file is also created in the build folder and when I pushed to the Azure, it worked like charm.当我构建我的应用程序时,这个 web.config 文件也在构建文件夹中创建,当我推送到 Azure 时,它就像魅力一样工作。
first, Chrome, Edge, FireFox on iOS are all pseudo browsers, not real ones.首先,iOS 上的 Chrome、Edge、FireFox 都是伪浏览器,不是真正的浏览器。 They just use a webview so you can sync passwords and favorites.
他们只使用网络视图,因此您可以同步密码和收藏夹。 Safari is the only browser allowed on iOS.
Safari 是唯一允许在 iOS 上使用的浏览器。
beforeInstallPrompt is not supported on iOS, you have to manually prompt. iOS 不支持 beforeInstallPrompt,需要手动提示。
For Chrome on Android you can remotely debug using a USB cable.对于 Android 上的 Chrome,您可以使用 USB 电缆远程调试。 This might tell you what is holding you back.
这可能会告诉您是什么阻碍了您。
Possibly not referencing the service worker or manifest correctly seems to be a very common issue when deploying to a server from localhost.从本地主机部署到服务器时,可能没有正确引用服务工作者或清单似乎是一个非常常见的问题。
I have a library available that might help you out.我有一个可用的图书馆,可以帮助你。 https://love2dev.com/pwa/add-to-homescreen-library/
https://love2dev.com/pwa/add-to-homescreen-library/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.