Fixing bugs with push notifications

This commit is contained in:
Jon Staab
2026-01-28 13:35:50 -08:00
parent bf6abd301c
commit 000344a942
6 changed files with 81 additions and 78 deletions
+6 -6
View File
@@ -4,13 +4,13 @@ const config: CapacitorConfig = {
appId: "social.flotilla",
appName: "Flotilla",
webDir: "build",
server: {
androidScheme: "https",
},
android: {
adjustMarginsForEdgeToEdge: false,
},
plugins: {
CapacitorHttp: {
enabled: true,
},
SystemBars: {
insetsHandling: "disable",
},
@@ -26,10 +26,10 @@ const config: CapacitorConfig = {
autoClear: true,
},
},
// Use this for live reload https://capacitorjs.com/docs/guides/live-reload
server: {
url: "http://192.168.1.148:1847",
cleartext: true,
// Use this for live reload https://capacitorjs.com/docs/guides/live-reload
// url: "http://192.168.1.17:1847",
// cleartext: true,
},
}
+1 -1
View File
@@ -79,7 +79,7 @@
"husky": "^9.1.7",
"idb": "^8.0.3",
"nostr-signer-capacitor-plugin": "^0.0.4",
"nostr-tools": "^2.19.4",
"nostr-tools": "^2.19.4",
"prettier-plugin-tailwindcss": "^0.6.14",
"qr-scanner": "^1.4.2",
"qrcode": "^1.5.4",
+7 -7
View File
@@ -5601,7 +5601,7 @@ snapshots:
rimraf: 4.4.1
semver: 7.7.3
tar: 6.2.1
tslib: 2.6.2
tslib: 2.8.1
xml2js: 0.5.0
transitivePeerDependencies:
- supports-color
@@ -5849,7 +5849,7 @@ snapshots:
'@ionic/utils-array@2.1.6':
dependencies:
debug: 4.3.4
tslib: 2.6.2
tslib: 2.8.1
transitivePeerDependencies:
- supports-color
@@ -5858,7 +5858,7 @@ snapshots:
'@types/fs-extra': 8.1.5
debug: 4.3.4
fs-extra: 9.1.0
tslib: 2.6.2
tslib: 2.8.1
transitivePeerDependencies:
- supports-color
@@ -5876,7 +5876,7 @@ snapshots:
debug: 4.3.4
signal-exit: 3.0.7
tree-kill: 1.2.2
tslib: 2.6.2
tslib: 2.8.1
transitivePeerDependencies:
- supports-color
@@ -5894,7 +5894,7 @@ snapshots:
'@ionic/utils-stream@3.1.6':
dependencies:
debug: 4.3.4
tslib: 2.6.2
tslib: 2.8.1
transitivePeerDependencies:
- supports-color
@@ -5914,7 +5914,7 @@ snapshots:
'@ionic/utils-terminal': 2.3.4
cross-spawn: 7.0.6
debug: 4.3.4
tslib: 2.6.2
tslib: 2.8.1
transitivePeerDependencies:
- supports-color
@@ -5939,7 +5939,7 @@ snapshots:
slice-ansi: 4.0.0
string-width: 4.2.3
strip-ansi: 6.0.1
tslib: 2.6.2
tslib: 2.8.1
untildify: 4.0.0
wrap-ansi: 7.0.0
transitivePeerDependencies:
+1 -1
View File
@@ -26,7 +26,7 @@
<link rel="apple-touch-icon" sizes="180x180" href="/icons/apple-touch-icon-180x180.png" />
%sveltekit.head%
</head>
<body data-sveltekit-preload-data="hover">
<body data-sveltekit-preload-data="hover" data-sveltekit-preload-code="eager">
<div style="display: contents">%sveltekit.body%</div>
<script
defer
+2 -2
View File
@@ -13,9 +13,9 @@
</script>
<Link replaceState href={path}>
<CardButton class="btn-neutral shadow-md">
<CardButton class="btn-neutral shadow-md bg-alt">
{#snippet icon()}
<RelayIcon {url} size={12} />
<RelayIcon {url} size={12} class="rounded-full" />
{/snippet}
{#snippet title()}
<div class="flex gap-1">
+64 -61
View File
@@ -31,10 +31,10 @@ import {
identity,
now,
groupBy,
postJson,
nth,
nthEq,
maybe,
throttle,
} from "@welshman/lib"
import type {TrustedEvent, Filter} from "@welshman/util"
import {deriveEventsByIdByUrl} from "@welshman/store"
@@ -349,7 +349,6 @@ interface IPushAdapter {
request: (prompt?: boolean) => Promise<string>
disable: () => Promise<void>
enable: () => Promise<void>
stop: () => Promise<void>
}
if (Capacitor.isNativePlatform()) {
@@ -411,14 +410,32 @@ class CapacitorNotifications implements IPushAdapter {
}
if (!subscription) {
const channel = Capacitor.getPlatform() === "ios" ? "apns" : "fcm"
const url = buildUrl(PUSH_SERVER, "subscription", channel)
const json = await postJson(url, {token}, {signal})
try {
const channel = Capacitor.getPlatform() === "ios" ? "apns" : "fcm"
const url = buildUrl(PUSH_SERVER, "subscription", channel)
const res = await fetch(url, {
signal,
method: "POST",
body: JSON.stringify({token}),
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
})
if (json?.callback && json?.key) {
notificationState.update(assoc("subscription", json))
} else {
console.warn("Failed to register with push server")
if (!res.ok) {
console.warn(`Failed to register with push server (status ${res.status})`)
} else {
const json = await res.json()
if (json?.callback && json?.key) {
notificationState.update(assoc("subscription", json))
} else {
console.warn("Failed to register with push server (bad response)")
}
}
} catch (e) {
console.warn("Failed to register with push server:", e)
}
}
}
@@ -439,7 +456,6 @@ class CapacitorNotifications implements IPushAdapter {
}
_syncRelay = async (relay: string, key: string, filters: Filter[], ignore: Filter[] = []) => {
console.log(`=== syncing ${relay} ${key}`)
const {subscription} = notificationState.get()
if (!subscription) {
@@ -486,8 +502,6 @@ class CapacitorNotifications implements IPushAdapter {
}
_unsyncRelay = async (relay: string, keys: string[]) => {
console.log(`=== unsyncing ${relay} ${keys.join(", ")}`)
const stuff = await this._getPushStuff(relay)
if (!stuff) {
@@ -517,7 +531,7 @@ class CapacitorNotifications implements IPushAdapter {
signal.addEventListener(
"abort",
merged([userSpaceUrls, notificationSettings, userSettingsValues]).subscribe(
([$userSpaceUrls, {spaces, mentions}, {muted_rooms}]) => {
throttle(3000, ([$userSpaceUrls, {spaces, mentions}, {muted_rooms}]) => {
const filters = [{kinds: MESSAGE_KINDS}, makeCommentFilter(CONTENT_KINDS)]
for (const url of $userSpaceUrls) {
@@ -545,7 +559,7 @@ class CapacitorNotifications implements IPushAdapter {
}
}
}
},
}),
),
)
}
@@ -554,7 +568,7 @@ class CapacitorNotifications implements IPushAdapter {
signal.addEventListener(
"abort",
merged([userMessagingRelayList, notificationSettings]).subscribe(
([$userMessagingRelayList, {messages}]) => {
throttle(3000, ([$userMessagingRelayList, {messages}]) => {
for (const url of getRelaysFromList($userMessagingRelayList)) {
if (messages) {
this._syncRelay(url, "messages", [{kinds: DM_KINDS, "#p": [pubkey.get()!]}])
@@ -562,24 +576,29 @@ class CapacitorNotifications implements IPushAdapter {
this._unsyncRelay(url, ["messages"])
}
}
},
}),
),
)
}
async enable() {
this._controller = new AbortController()
if (!this._controller) {
this._controller = new AbortController()
try {
await this._syncServer(this._controller.signal)
await this._syncSpaceSubscription(this._controller.signal)
await this._syncMessageSubscription(this._controller.signal)
} catch (e) {
console.error(e)
try {
await this._syncServer(this._controller.signal)
await this._syncSpaceSubscription(this._controller.signal)
await this._syncMessageSubscription(this._controller.signal)
} catch (e) {
console.error(e)
}
}
}
async disable() {
this._controller?.abort()
this._controller = undefined
const {subscription} = notificationState.get()
if (subscription) {
@@ -602,11 +621,6 @@ class CapacitorNotifications implements IPushAdapter {
notificationState.set({})
}
async stop() {
this._controller?.abort()
this._controller = undefined
}
}
class WebNotifications implements IPushAdapter {
@@ -645,30 +659,28 @@ class WebNotifications implements IPushAdapter {
}
async enable() {
this._unsubscriber = onNotification(event => {
const {push, messages, mentions, spaces} = notificationSettings.get()
if (!this._unsubscriber) {
this._unsubscriber = onNotification(event => {
const {push, messages, mentions, spaces} = notificationSettings.get()
if (push && document.hidden && Notification?.permission === "granted") {
if (messages && matchFilters(dmFilters, event)) {
this._notify(event, "New direct message", "Someone sent you a direct message.")
} else if (
mentions &&
event.pubkey !== pubkey.get() &&
getPubkeyTagValues(event.tags).includes(pubkey.get()!)
) {
this._notify(event, "Someone mentioned you", "Someone tagged you in a message.")
} else if (spaces) {
this._notify(event, "New activity", "Someone posted a new message.")
if (push && document.hidden && Notification?.permission === "granted") {
if (messages && matchFilters(dmFilters, event)) {
this._notify(event, "New direct message", "Someone sent you a direct message.")
} else if (
mentions &&
event.pubkey !== pubkey.get() &&
getPubkeyTagValues(event.tags).includes(pubkey.get()!)
) {
this._notify(event, "Someone mentioned you", "Someone tagged you in a message.")
} else if (spaces) {
this._notify(event, "New activity", "Someone posted a new message.")
}
}
}
})
})
}
}
async disable() {
// pass
}
async stop() {
this._unsubscriber?.()
this._unsubscriber = undefined
}
@@ -696,27 +708,18 @@ export class Push {
static disable() {
return Push._getAdapter().disable()
}
static enable() {
return Push._getAdapter().enable()
}
static stop() {
return Push._getAdapter().stop()
}
static sync() {
const adapter = Push._getAdapter()
const unsubscriber = notificationSettings.subscribe($notificationSettings => {
if ($notificationSettings.push) {
adapter.enable()
return notificationSettings.subscribe(({push}) => {
if (push) {
Push.enable()
} else {
adapter.disable()
Push.disable()
}
})
return () => {
unsubscriber()
adapter.stop()
}
}
}