Skip to content

Commit 903d62e

Browse files
committed
Add error comment
1 parent adef626 commit 903d62e

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

web/lib/service/web-push.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
import {useEffect} from "react";
2-
import {urlBase64ToUint8Array} from "common/util/parse";
3-
import {api} from "web/lib/api";
4-
import {useUser} from "web/hooks/use-user";
5-
import {isNativeMobile} from "web/lib/util/webview";
1+
import {useEffect} from "react"
2+
import {urlBase64ToUint8Array} from "common/util/parse"
3+
import {api} from "web/lib/api"
4+
import {useUser} from "web/hooks/use-user"
5+
import {isNativeMobile} from "web/lib/util/webview"
66

77
const vapidPublicKey = 'BF80q7LrDa4a5ksS2BZrX6PPvL__y0jCNvNqyUzvk8Y4ofTdrS0kRnKfGpClCQAHWmcPHIUmWq8jgQ4ROquSpJQ'
88

99
export default function WebPush() {
10-
const user = useUser(); // authenticated user
10+
const user = useUser() // authenticated user
1111
const isWeb = typeof window !== 'undefined' && 'serviceWorker' in navigator && !isNativeMobile()
1212
useEffect(() => {
13-
if (!user?.id || !isWeb) return;
13+
if (!user?.id || !isWeb) return
1414
console.log('WebPush', user)
1515

1616
const registerPush = async () => {
@@ -19,14 +19,16 @@ export default function WebPush() {
1919
.then(async (registration) => {
2020
console.log('Service worker registered:', registration)
2121

22-
const permission = await Notification.requestPermission();
22+
// May need to ask for permission from a button the user clicks, per this error:
23+
// The Notification permission may only be requested from inside a short-running user-generated event handler.
24+
const permission = await Notification.requestPermission()
2325
if (permission !== 'granted') {
2426
console.log('Notification permission denied')
2527
return
2628
}
2729

2830
// Check if already subscribed
29-
const existing = await registration.pushManager.getSubscription();
31+
const existing = await registration.pushManager.getSubscription()
3032
if (existing) {
3133
console.log('Already subscribed:', existing)
3234
return
@@ -45,7 +47,7 @@ export default function WebPush() {
4547
console.error('SW registration failed:', err)
4648
return
4749
})
48-
};
50+
}
4951

5052
registerPush()
5153
}, [user?.id, isWeb])

0 commit comments

Comments
 (0)