Update VAPID key handling and improve push notification encoding
Some checks failed
CI / ci (push) Failing after 1m14s

This commit is contained in:
2026-08-25 23:50:28 +02:00
parent ab42e16eca
commit b27561c91e
5 changed files with 30 additions and 23 deletions

View File

@@ -13,6 +13,13 @@ function urlBase64ToUint8Array(base64String: string): Uint8Array {
return array
}
function arrayBufferToBase64Url(buffer: ArrayBuffer): string {
const bytes = new Uint8Array(buffer)
let binary = ""
for (const b of bytes) binary += String.fromCharCode(b)
return btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "")
}
export function usePushNotifications() {
const [isSubscribed, setIsSubscribed] = useState(false)
const [isSupported, setIsSupported] = useState(false)
@@ -59,8 +66,8 @@ export function usePushNotifications() {
await push.subscribe({
endpoint: sub.endpoint,
p256dh: btoa(String.fromCharCode(...new Uint8Array(key))),
auth: btoa(String.fromCharCode(...new Uint8Array(auth))),
p256dh: arrayBufferToBase64Url(key),
auth: arrayBufferToBase64Url(auth),
})
},
onSuccess: () => setIsSubscribed(true),