Skip to content

Commit

Permalink
feat: throw error
Browse files Browse the repository at this point in the history
  • Loading branch information
friendlysxw committed Jan 30, 2024
1 parent 3bd034c commit 1da3afc
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion apps/u3/src/utils/pwa/WebPushService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,23 @@ class WebPushService {
}

static async subscribe() {
const applicationServerKey = process.env.REACT_APP_VAPID_PUBLIC_KEY;
if (!applicationServerKey) {
throw new Error('VAPID public key not found');
}
const registration = await navigator.serviceWorker.ready;
if (!registration) {
throw new Error('Service Worker not ready');
}
if (!registration?.pushManager) {
throw new Error('PushManager not ready');
}
if (!registration?.pushManager?.subscribe) {
throw new Error('PushManager subscribe not ready');
}
const subscription = await registration.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: process.env.REACT_APP_VAPID_PUBLIC_KEY,
applicationServerKey,
});
return subscription;
}
Expand Down

0 comments on commit 1da3afc

Please sign in to comment.