push notifications: ask for user permission;

This commit is contained in:
Dobromir Popov
2024-04-08 00:29:27 +03:00
parent 6bfbd73162
commit 3ddb514d9c
4 changed files with 88 additions and 33 deletions

View File

@ -14,39 +14,12 @@ import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs'
// Use of the <SessionProvider> is mandatory to allow components that call
// `useSession()` anywhere in your application to access the `session` object.
export const metadata: Metadata = {
title: "Специално Свидетелстване София",
description: "Специално Свидетелстване София",
viewport: "width=device-width, initial-scale=1",
appleWebApp: true,
/// manifest: "/manifest.json",
// manifest: {
// theme_color: "#000000",
// background_color: "#ffffff",
// icons: [
// {
// purpose: "maskable",
// sizes: "512x512",
// src: "favicon.png",
// type: "image/png"
// },
// {
// purpose: "any",
// sizes: "512x512",
// src: "favicon.png",
// type: "image/png"
// }
// ],
// orientation: "any",
// display: "standalone",
// dir: "auto",
// lang: "en-US",
// name: "Специално Свидетелстване София",
// short_name: "ССС",
// start_url: "/",
// scope: "/cart"
// },
}
// export const metadata: Metadata = {
// title: "Специално Свидетелстване София",
// description: "Специално Свидетелстване София",
// viewport: "width=device-width, initial-scale=1",
// appleWebApp: true,
// }
export default function App({
@ -62,6 +35,36 @@ export default function App({
}, []);
// PUSH NOTIFICATIONS
useEffect(() => {
// Function to ask for Notification permission
const askForNotificationPermission = async () => {
// Check if the browser supports service workers and push notifications
if ('serviceWorker' in navigator && 'PushManager' in window) {
try {
// Wait for service worker registration
const registration = await navigator.serviceWorker.ready;
// Ask for notification permission
const permission = await Notification.requestPermission();
if (permission === 'granted') {
console.log('Notification permission granted.');
// TODO: Subscribe the user to push notifications here
} else {
console.log('Notification permission not granted.');
}
} catch (error) {
console.error('Error during service worker registration:', error);
}
} else {
console.log('Service Worker or Push notifications not supported in this browser.');
}
};
// Call the function to ask for permission on first load
askForNotificationPermission();
}, []);
return (
<>
<SessionProvider session={session} >