1
0
Fork 0
mirror of https://github.com/binwiederhier/ntfy.git synced 2024-12-26 11:42:29 +01:00

Set PWA web push enabled on launch instead

This commit is contained in:
nimbleghost 2023-06-25 21:57:08 +02:00
parent a8d3297c4e
commit 833293ad77
2 changed files with 22 additions and 8 deletions

View file

@ -2,7 +2,7 @@ import api from "./Api";
import notifier from "./Notifier";
import prefs from "./Prefs";
import db from "./db";
import { isLaunchedPWA, topicUrl } from "./utils";
import { topicUrl } from "./utils";
class SubscriptionManager {
constructor(dbImpl) {
@ -27,14 +27,15 @@ class SubscriptionManager {
* It is important to note that "mutedUntil" must be part of the where() query, otherwise the Dexie live query
* will not react to it, and the Web Push topics will not be updated when the user mutes a topic.
*/
async webPushTopics(isStandalone = isLaunchedPWA(), pushPossible = notifier.pushPossible()) {
async webPushTopics(pushPossible = notifier.pushPossible()) {
if (!pushPossible) {
return [];
}
// the Promise.resolve wrapper is not superfluous, without it the live query breaks:
// https://dexie.org/docs/dexie-react-hooks/useLiveQuery()#calling-non-dexie-apis-from-querier
if (!(isStandalone || (await Promise.resolve(prefs.webPushEnabled())))) {
const enabled = await Promise.resolve(prefs.webPushEnabled());
if (!enabled) {
return [];
}

View file

@ -12,6 +12,7 @@ import accountApi from "../app/AccountApi";
import { UnauthorizedError } from "../app/errors";
import useWebPushListener from "../app/WebPush";
import notifier from "../app/Notifier";
import prefs from "../app/Prefs";
/**
* Wire connectionManager and subscriptionManager so that subscriptions are updated when the connection
@ -135,15 +136,14 @@ export const useAutoSubscribe = (subscriptions, selected) => {
}, [params, subscriptions, selected, hasRun]);
};
export const useWebPushTopics = () => {
export const useStandaloneAutoWebPushSubscribe = () => {
const matchMedia = window.matchMedia("(display-mode: standalone)");
const [isStandalone, setIsStandalone] = useState(isLaunchedPWA());
const [pushPossible, setPushPossible] = useState(notifier.pushPossible());
useEffect(() => {
const handler = (evt) => {
console.log(`[useWebPushTopics] App is now running ${evt.matches ? "standalone" : "in the browser"}`);
console.log(`[useStandaloneAutoWebPushSubscribe] App is now running ${evt.matches ? "standalone" : "in the browser"}`);
setIsStandalone(evt.matches);
};
@ -154,6 +154,17 @@ export const useWebPushTopics = () => {
};
});
useEffect(() => {
if (isStandalone) {
console.log(`[useStandaloneAutoWebPushSubscribe] Turning on web push automatically`);
prefs.setWebPushEnabled(true);
}
}, [isStandalone]);
};
export const useWebPushTopics = () => {
const [pushPossible, setPushPossible] = useState(notifier.pushPossible());
useEffect(() => {
const handler = () => {
const newPushPossible = notifier.pushPossible();
@ -173,9 +184,9 @@ export const useWebPushTopics = () => {
});
const topics = useLiveQuery(
async () => subscriptionManager.webPushTopics(isStandalone, pushPossible),
async () => subscriptionManager.webPushTopics(pushPossible),
// invalidate (reload) query when these values change
[isStandalone, pushPossible]
[pushPossible]
);
useWebPushListener(topics);
@ -202,6 +213,8 @@ const stopWorkers = () => {
};
export const useBackgroundProcesses = () => {
useStandaloneAutoWebPushSubscribe();
useEffect(() => {
console.log("[useBackgroundProcesses] mounting");
startWorkers();