From 0bbff458bd4337c6d3c5b7b89bf592f1c06ffb9d Mon Sep 17 00:00:00 2001 From: userAdityaa Date: Tue, 26 May 2026 11:32:32 +0530 Subject: [PATCH] fix: turn on notification defaults and prompt on first DM visit --- .../EnableNotificationsPrompt.svelte | 58 +++++++++++++++++++ src/app/core/state.ts | 6 +- src/app/util/notifications.ts | 46 ++++++++++++++- src/routes/chat/[chat]/+page.svelte | 12 ++++ 4 files changed, 118 insertions(+), 4 deletions(-) create mode 100644 src/app/components/EnableNotificationsPrompt.svelte diff --git a/src/app/components/EnableNotificationsPrompt.svelte b/src/app/components/EnableNotificationsPrompt.svelte new file mode 100644 index 00000000..0b83db69 --- /dev/null +++ b/src/app/components/EnableNotificationsPrompt.svelte @@ -0,0 +1,58 @@ + + + + + + + + + Enable notifications + + + + Get notified when you receive new direct messages, even when Flotilla is in the background. + + + + + + + + diff --git a/src/app/core/state.ts b/src/app/core/state.ts index 0496d655..8565e7d6 100644 --- a/src/app/core/state.ts +++ b/src/app/core/state.ts @@ -417,9 +417,9 @@ export const device = withGetter(writable(randomId())) export const notificationSettings = withGetter( writable({ - push: false, - sound: false, - badge: false, + push: true, + sound: true, + badge: true, spaces: true, mentions: true, messages: true, diff --git a/src/app/util/notifications.ts b/src/app/util/notifications.ts index 0bc5cb3c..eaadb529 100644 --- a/src/app/util/notifications.ts +++ b/src/app/util/notifications.ts @@ -1,4 +1,5 @@ import {derived, get, writable} from "svelte/store" +import {Capacitor} from "@capacitor/core" import {Badge} from "@capawesome/capacitor-badge" import {synced, throttled, withGetter} from "@welshman/store" import {pubkey, tracker, repository, relaysByUrl} from "@welshman/app" @@ -10,6 +11,7 @@ import {makeSpacePath, makeRoomPath, makeSpaceChatPath, makeChatPath} from "@app import { CONTENT_KINDS, notificationSettings, + pushState, chatsById, userGroupList, getSpaceUrlsFromGroupList, @@ -18,7 +20,49 @@ import { } from "@app/core/state" import {kv} from "@app/core/storage" import {page} from "$app/stores" -export {Push} from "@app/util/push" +import {Push} from "@app/util/push" + +export {Push} + +export const areNotificationsEnabled = () => { + const {push, messages} = notificationSettings.get() + + if (!push || !messages) { + return false + } + + if (Capacitor.isNativePlatform()) { + return Boolean(pushState.get().token) + } + + return Notification?.permission === "granted" +} + +export const enableNotifications = async () => { + const permission = await Push.request() + + if (!permission.startsWith("granted")) { + return false + } + + const current = notificationSettings.get() + + notificationSettings.set({ + ...current, + push: true, + badge: true, + sound: Capacitor.isNativePlatform() ? current.sound : true, + messages: true, + }) + + return true +} + +export const dmNotificationsPrompted = synced({ + key: "dmNotificationsPrompted", + defaultValue: false, + storage: kv, +}) // Checked state diff --git a/src/routes/chat/[chat]/+page.svelte b/src/routes/chat/[chat]/+page.svelte index 4239f43a..83fb7675 100644 --- a/src/routes/chat/[chat]/+page.svelte +++ b/src/routes/chat/[chat]/+page.svelte @@ -1,13 +1,25 @@