Skip to content

Commit 0c76a3e

Browse files
bndct-devopsCopilot
andcommitted
fix: support safari push subscription keys
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 4f16dde commit 0c76a3e

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

frontend/src/api.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,12 @@ export async function getPushVapidKey() {
288288
return data.publicKey
289289
}
290290

291+
function bytesToBase64Url(bytes) {
292+
let binary = ''
293+
bytes.forEach(b => { binary += String.fromCharCode(b) })
294+
return btoa(binary).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/g, '')
295+
}
296+
291297
export async function subscribePush(profileId) {
292298
if (!('PushManager' in window) || !navigator.serviceWorker) return false
293299
try {
@@ -304,14 +310,18 @@ export async function subscribePush(profileId) {
304310
applicationServerKey: rawKey,
305311
})
306312
const json = sub.toJSON()
313+
const p256dhKey = sub.getKey?.('p256dh')
314+
const authKey = sub.getKey?.('auth')
315+
const p256dh = p256dhKey ? bytesToBase64Url(new Uint8Array(p256dhKey)) : json.keys?.p256dh
316+
const auth = authKey ? bytesToBase64Url(new Uint8Array(authKey)) : json.keys?.auth
307317
const res = await authFetch(base + '/api/push/subscribe', {
308318
method: 'POST',
309319
headers: { 'Content-Type': 'application/json' },
310320
body: JSON.stringify({
311321
profileId,
312322
endpoint: json.endpoint,
313-
p256dh: json.keys.p256dh,
314-
auth: json.keys.auth,
323+
p256dh,
324+
auth,
315325
}),
316326
})
317327
if (!res.ok) {

0 commit comments

Comments
 (0)