Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,6 @@ async function openNotificationSettings(): Promise<void> {
}
}

function arrayBufferToBase64(buffer: ArrayBuffer): string {
const bytes = new Uint8Array(buffer)
let binary = ''
bytes.forEach((b) => (binary += String.fromCharCode(b)))
return btoa(binary)
}

async function disableWebPush(client: any): Promise<void> {
try {
const { webPushServices } = connectionStore.getState()
Expand All @@ -66,13 +59,12 @@ async function disableWebPush(client: any): Promise<void> {
const subscription = await swReg.pushManager.getSubscription()
if (!subscription) return

const p256dhKey = subscription.getKey('p256dh')
const authKey = subscription.getKey('auth')
if (!p256dhKey || !authKey) return
const json = subscription.toJSON()
const p256dh = json.keys?.p256dh
const auth = json.keys?.auth
if (!p256dh || !auth) return

const p256dh = arrayBufferToBase64(p256dhKey)
const auth = arrayBufferToBase64(authKey)
const notificationId = `${subscription.endpoint}#${p256dh}#${auth}`
const notificationId = `${json.endpoint ?? subscription.endpoint}#${p256dh}#${auth}`

await client.webPush.disableSubscription(service.appId, 'webpush', notificationId)
connectionStore.getState().setWebPushEnabled(false)
Expand Down
19 changes: 5 additions & 14 deletions apps/fluux/src/hooks/useWebPush.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,15 @@ async function registerPush(
console.log('[WebPush] New subscription created, endpoint:', subscription.endpoint)
}

const endpoint = subscription.endpoint
const p256dhKey = subscription.getKey('p256dh')
const authKey = subscription.getKey('auth')
if (!p256dhKey || !authKey) {
const json = subscription.toJSON()
const endpoint = json.endpoint ?? subscription.endpoint
const p256dh = json.keys?.p256dh
const auth = json.keys?.auth
if (!p256dh || !auth) {
console.error('[WebPush] Missing subscription keys')
return
}

const p256dh = arrayBufferToBase64(p256dhKey)
const auth = arrayBufferToBase64(authKey)

console.log('[WebPush] Registering with XMPP server, endpoint:', endpoint)
await client.webPush.registerSubscription(endpoint, p256dh, auth, service.appId)
console.log('[WebPush] Registration complete!')
Expand Down Expand Up @@ -151,10 +149,3 @@ function urlBase64ToUint8Array(base64String: string): Uint8Array {
const rawData = atob(base64)
return Uint8Array.from(rawData, (char) => char.charCodeAt(0))
}

function arrayBufferToBase64(buffer: ArrayBuffer): string {
const bytes = new Uint8Array(buffer)
let binary = ''
bytes.forEach((b) => (binary += String.fromCharCode(b)))
return btoa(binary)
}
Loading