PWA 应用程序 beforeInstallPrompt 不触发其他浏览器只触发 Chrome 浏览器

问题描述

在 PWA Reactjs 应用程序中 beforeinstallprompt 仅在 Chrome 移动和桌面浏览器中触发。我想像 Chrome 一样触发或触发其他浏览器。我做错了什么。

在 App.js 中添加带有 useEffect 的 EventListener。

   useEffect(() => {
        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
            setInstallable(true);
        });

        window.addEventListener('appinstalled',() => {
            // Log install to analytics
            console.log('INSTALL: Success');
        });
    },[]);

但不触发其他浏览器只触发chrome浏览器...

解决方法

beforeinstallprompt 事件目前仅在 Chrome、Edge 和 Samsung Internet 中触发。 Safari 允许用户安装 PWA,但用户必须通过点击共享按钮,然后添加到主屏幕来自己安装。