From 48d1f7887d475dae5f4e894660f5fff9b5b3afee Mon Sep 17 00:00:00 2001 From: nimbleghost <132819643+nimbleghost@users.noreply.github.com> Date: Sat, 24 Jun 2023 20:18:02 +0200 Subject: [PATCH] Default web push to enabled on PWAs --- web/src/app/Prefs.js | 3 ++- web/src/app/utils.js | 13 +++++++++++++ web/src/components/Preferences.jsx | 4 ++-- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/web/src/app/Prefs.js b/web/src/app/Prefs.js index f580c620..632c9adc 100644 --- a/web/src/app/Prefs.js +++ b/web/src/app/Prefs.js @@ -1,4 +1,5 @@ import db from "./db"; +import { isLaunchedPWA } from "./utils"; class Prefs { constructor(dbImpl) { @@ -34,7 +35,7 @@ class Prefs { async webPushEnabled() { const webPushEnabled = await this.db.prefs.get("webPushEnabled"); - return webPushEnabled?.value ?? false; + return webPushEnabled?.value ?? isLaunchedPWA(); } async setWebPushEnabled(enabled) { diff --git a/web/src/app/utils.js b/web/src/app/utils.js index 244d3321..afb153ff 100644 --- a/web/src/app/utils.js +++ b/web/src/app/utils.js @@ -263,3 +263,16 @@ export const urlB64ToUint8Array = (base64String) => { } return outputArray; }; + +export const isLaunchedPWA = () => { + if (window.matchMedia("(display-mode: standalone)").matches) { + return true; + } + + // iOS + if (window.navigator.standalone === true) { + return true; + } + + return false; +}; diff --git a/web/src/components/Preferences.jsx b/web/src/components/Preferences.jsx index e12fdadb..f6bd9963 100644 --- a/web/src/components/Preferences.jsx +++ b/web/src/components/Preferences.jsx @@ -36,7 +36,7 @@ import { Info } from "@mui/icons-material"; import { useOutletContext } from "react-router-dom"; import theme from "./theme"; import userManager from "../app/UserManager"; -import { playSound, shuffle, sounds, validUrl } from "../app/utils"; +import { isLaunchedPWA, playSound, shuffle, sounds, validUrl } from "../app/utils"; import session from "../app/Session"; import routes from "./routes"; import accountApi, { Permission, Role } from "../app/AccountApi"; @@ -86,7 +86,7 @@ const Notifications = () => { - {notifier.pushPossible() && } + {!isLaunchedPWA() && notifier.pushPossible() && } );