deepakjha14 / students-details-pwa

4 stars 1 forks source link

Safari compatibility #2

Closed ottopic closed 6 months ago

ottopic commented 6 months ago

I have tried to add Safari desktop compatibility but I same problem of randomly popup of notification acceptance. Can you help me?

navigator.serviceWorker
  .getRegistration('./ngsw-worker.js')
  .then((registration) => {
    requestPermission(registration);

  });

async function requestPermission(
  registration: ServiceWorkerRegistration | undefined,
) {
  return new Promise((resolve) => {
    const timeoutId = setInterval(() => {
      if (Notification.permission === 'granted') {
        handleComplete(Notification.permission);
      }
    }, 3000);

    const handleComplete = (permission: NotificationPermission) => {
      clearInterval(timeoutId);
      resolve(permission);

      if (permission === 'granted') {
        getToken(messaging, {
          vapidKey: '...',
          serviceWorkerRegistration: registration,
        })
          .then((currentToken) => {
            if (currentToken) {
              console.log(currentToken);
            } else {
              console.log(
                'No registration token available. Request permission to generate one.',
              );
            }
          })
          .catch((err) => {
            console.log('Error token ', err);
          });
      }
    };

    Notification.requestPermission(handleComplete)?.then?.(handleComplete);
  });
}