diff --git a/components/header.js b/components/header.js index 75e8c775..0f477b80 100644 --- a/components/header.js +++ b/components/header.js @@ -72,7 +72,7 @@ export default function Header ({ sub }) { onCompleted: (data) => { const notified = JSON.parse(localStorage.getItem('notified')) || false if (!notified && data.hasNewNotes) { - notification.show('you have new notifications') + notification.show('you have Stacker News notifications') } localStorage.setItem('notified', data.hasNewNotes) } diff --git a/components/notifications.js b/components/notifications.js index 9470ced7..c52c825d 100644 --- a/components/notifications.js +++ b/components/notifications.js @@ -301,10 +301,11 @@ export const NotificationProvider = ({ children }) => { const [isDefaultPermission, setIsDefaultPermission] = useState(isSupported ? window.Notification.permission === 'default' : undefined) const [isGranted, setIsGranted] = useState(isSupported ? window.Notification.permission === 'granted' : undefined) const me = useMe() + const router = useRouter() const show_ = (title, options) => { const icon = '/android-chrome-24x24.png' - window.Notification(title, { icon, ...options }) + return new window.Notification(title, { icon, ...options }) } const show = useCallback((...args) => { @@ -317,15 +318,15 @@ export const NotificationProvider = ({ children }) => { setIsDefaultPermission(window.Notification.permission === 'default') if (result === 'granted') { setIsGranted(result === 'granted') - show_('you have enabled notifications') + show_('Stacker News notifications enabled') } }) }, [isDefaultPermission]) useEffect(() => { - if (!me || !isSupported || !isDefaultPermission) return + if (!me || !isSupported || !isDefaultPermission || router.pathname !== '/notifications') return requestPermission() - }, []) + }, [router?.pathname]) const ctx = { isBrowser, isSupported, isDefaultPermission, isGranted, show }