mirror of
https://github.com/HSLdevcom/digitransit-ui
synced 2025-07-28 08:05:18 +02:00
47 lines
1.3 KiB
JavaScript
47 lines
1.3 KiB
JavaScript
import { useState, useEffect } from 'react';
|
|
|
|
navigator.serviceWorker?.register('/sw.js');
|
|
|
|
const usePushNotification = config => {
|
|
const [notification, setNotification] = useState(null);
|
|
|
|
useEffect(() => {
|
|
if (notification) {
|
|
notification.onclick = () => {
|
|
window.focus();
|
|
notification.close();
|
|
};
|
|
}
|
|
}, [notification]);
|
|
|
|
const createNotification = (title, content) => {
|
|
if ('Notification' in window && Notification.permission === 'granted') {
|
|
navigator.serviceWorker?.ready.then(registration => {
|
|
const newNotification = registration.showNotification(title, {
|
|
body: content,
|
|
});
|
|
setNotification(newNotification);
|
|
});
|
|
}
|
|
};
|
|
const notificationConsent = () => {
|
|
if (config.experimental.notifications && 'Notification' in window) {
|
|
if (
|
|
Notification.permission !== 'denied' &&
|
|
Notification.permission !== 'granted'
|
|
) {
|
|
Notification.requestPermission().then(permission => {
|
|
if (permission === 'granted') {
|
|
createNotification(
|
|
'Notifications Approved',
|
|
'Notifications are now approved',
|
|
);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
};
|
|
|
|
return { createNotification, notificationConsent };
|
|
};
|
|
export { usePushNotification };
|