File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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+
291297export 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 ) {
You can’t perform that action at this time.
0 commit comments