简体   繁体   English

PWA 应用程序 beforeInstallPrompt 未在移动浏览器中触发但可在普通桌面浏览器中运行

[英]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.

相关问题 PWA 应用 beforeInstallPrompt 不触发其他浏览器只会触发 Chrome 浏览器 - PWA app beforeInstallPrompt not firing other browseres only fire Chrome browser 在桌面浏览器窗口中的不同视口[移动设备,平板电脑,桌面]中呈现网站 - Rendering website in different viewports [ Mobile, Tablet, Desktop ] on Desktop Browser window 无法使用PWA中的移动浏览器访问移动摄像头和麦克风(反应js) - Not able to Access the Mobile camera and Microphone using mobile browser in PWA(react js) ReactJS:如何确定应用程序是在移动浏览器还是桌面浏览器上查看 - ReactJS: How to determine if the application is being viewed on mobile or desktop browser reactjs应用程序在桌面上正常运行,但在移动设备上不可见 - reactjs app working properly on desktop but not visible on mobile 为什么提取在我的移动浏览器上不起作用? - Why is fetch not working on my mobile browser? 浏览器历史记录在电子应用程序中不起作用 - Browser history not working in electron app 通过手机浏览器访问PWA然后安装时,有没有办法持久化localstorage或者其他存储方式 - Is there a way of persisting localstorage or another method of storage when you visit a PWA through a mobile browser and then installing it React Web App路由未在移动浏览器上加载 - React web app route not loading on mobile browser 无法从 Web 浏览器安装 PWA - Can't install PWA from web browser
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM