Как я могу показать оповещение на рабочем столе, используя извещение? - PullRequest
0 голосов
/ 06 февраля 2019

Как показать уведомление на рабочем столе от Chrome, Firefox или IE, когда браузер закрыт или открыт?Есть ли способ сделать это с помощью метода извещения?

1 Ответ

0 голосов
/ 07 февраля 2019

Вы можете попробовать проверить API уведомлений.

API уведомлений позволяет веб-страницам управлять отображением системных уведомлений конечному пользователю.Они находятся за пределами видового экрана контекста верхнего уровня, поэтому могут отображаться, даже если пользователь переключил вкладки или перешел в другое приложение.API разработан для совместимости с существующими системами уведомлений на разных платформах.

пример кода:

<!doctype html>
<head>
<script>
window.addEventListener('load', function () {
  // At first, let's check if we have permission for notification
  // If not, let's ask for it
  if (window.Notification && Notification.permission !== "granted") {
    Notification.requestPermission(function (status) {
      if (Notification.permission !== status) {
        Notification.permission = status;
      }
    });
  }

  var button = document.getElementsByTagName('button')[0];

  button.addEventListener('click', function () {
    // If the user agreed to get notified
    // Let's try to send ten notifications
    if (window.Notification && Notification.permission === "granted") {
      var i = 0;
      // Using an interval cause some browsers (including Firefox) are blocking notifications if there are too much in a certain time.
      var interval = window.setInterval(function () {
        // Thanks to the tag, we should only see the "Hi! 9" notification 
        var n = new Notification("Hi! " + i, {tag: 'soManyNotification'});
        if (i++ == 9) {
          window.clearInterval(interval);
        }
      }, 200);
    }

    // If the user hasn't told if he wants to be notified or not
    // Note: because of Chrome, we are not sure the permission property
    // is set, therefore it's unsafe to check for the "default" value.
    else if (window.Notification && Notification.permission !== "denied") {
      Notification.requestPermission(function (status) {
        // If the user said okay
        if (status === "granted") {
          var i = 0;
          // Using an interval cause some browsers (including Firefox) are blocking notifications if there are too much in a certain time.
          var interval = window.setInterval(function () {
            // Thanks to the tag, we should only see the "Hi! 9" notification 
            var n = new Notification("Hi! " + i, {tag: 'soManyNotification'});
            if (i++ == 9) {
              window.clearInterval(interval);
            }
          }, 200);
        }

        // Otherwise, we can fallback to a regular modal alert
        else {
          alert("Hi!");
        }
      });
    }

    // If the user refuses to get notified
    else {
      // We can fallback to a regular modal alert
      alert("Hi!");
    }
  });
});
</script>
</head>
<body>
<button>Notify me!</button>
</body>
</html>

Ссылки:

(1) API уведомлений

(2) Использование API уведомлений

...