0

I have the following JS code used to send the desktop notification to the user, but it will only run when the page is open in the browser. Is it possible to show the desktop notification even when the page is not open, but has been previously opened (like Facebook is doing).

    document.addEventListener('DOMContentLoaded', function () {
        if (Notification.permission !== "granted")
            Notification.requestPermission();
    });

    function displayDesktopNotification(note, datetime) {
        if (!Notification) {
            alert('Desktop notifications not available in your browser. Try Chromium.');
            return;
        }


        if (Notification.permission !== "granted")
            Notification.requestPermission();
        else {
            var notification = new Notification('', {
                icon: '/Images/share.png',
                body: note + ' ' + datetime,
            });

            notification.onclick = function () {
                window.open("http://localhost/home?");
            };

        }

    }

    displayDesktopNotification("testing", "2017-07-06 09:56:00");
jsejcksn
  • 27,667
  • 4
  • 38
  • 62
Jin Yong
  • 42,698
  • 72
  • 141
  • 187
  • 2
    You are looking for Push Notifications. [this answer](https://stackoverflow.com/a/33976929/560593) talks about it. For when the browser is closed or you do not have an app running on the user's system, you would need a server backend to send a push request to the user's respective browser's / OS's push notification api endpoint. – Patrick Evans Jul 06 '17 at 02:05
  • there's also the Push API [documentation](https://developer.mozilla.org/en/docs/Web/API/Push_API) – Jaromanda X Jul 06 '17 at 02:05

0 Answers0