Coder Social home page Coder Social logo

Comments (1)

synox avatar synox commented on July 24, 2024

POC from https://web-push-book.gauntface.com/chapter-05/02-display-a-notification/

service-worker.js

  const examplePage = '/demos/notification-examples/example-page.html';

  function focusWindow(event) {
    const urlToOpen = new URL(examplePage, self.location.origin).href;
    const promiseChain = clients.matchAll({
      type: 'window',
      includeUncontrolled: true
    })
    .then((windowClients) => {
      let matchingClient = null;

      for (let i = 0; i < windowClients.length; i++) {
        const windowClient = windowClients[i];
        if (windowClient.url === urlToOpen) {
          matchingClient = windowClient;
          break;
        }
      }

      if (matchingClient) {
        return matchingClient.focus();
      } else {
        return clients.openWindow(urlToOpen);
      }
    });

    event.waitUntil(promiseChain);
  }

  self.addEventListener('notificationclick', function(event) {
    event.notification.close();
    focusWindow(event);
  });

  self.addEventListener('notificationclose', function(event) {
    const dismissedNotification = event.notification;
    event.waitUntil(Promise.resolve());
  });

index.html

  <!DOCTYPE html>
  <html>
  <body>
        <button onclick="mybutton()">Focus a Window OR Open</button>

    <script type="text/javascript">
      function registerServiceWorker() {
      return navigator.serviceWorker.register('service-worker.js')
      .then(function(registration) {
        console.log('Service worker successfully registered.');
        return registration;
      })
      .catch(function(err) {
        console.error('Unable to register service worker.', err);
      });
    }

      window.addEventListener('load', function() {

      return registerServiceWorker()
      .then(function(registration) {
        window.registration = registration
        navigator.serviceWorker.addEventListener('message', function(event) {
          console.log('Received a message from service worker: ', event.data);
          });  
        })
      })



      function mybutton(){
        const options = {
          body: 'Clicking on this notification will focus on an open window ' +
            'otherwise open a new one.',
          tag: 'focus-window'
        };
        registration.showNotification('Focus or Open a Window', options);
      
    };
    </script>
   
  </body>
  </html>

from void-mail.

Related Issues (11)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.