【问题标题】:Alert user of in-browser event when window minimized -- other than html5 notification窗口最小化时提醒用户浏览器内事件——除了 html5 通知
【发布时间】:2015-10-05 21:51:27
【问题描述】:

我有一个要求,即使窗口已最小化,我们也需要提醒用户浏览器中发生的某些事件。在浏览器中,我们使用来自 toastr 的 toast,如果用户已经在浏览器窗口中,这将非常有用。我们也有 html5 通知,所以在这种情况下这不是问题,但许多用户不一定会启用它。

如果窗口最小化,如果 Windows 中的浏览器任务栏项目开始闪烁或在 Mac 上弹跳,那就太棒了。我试图在事件发生时发送window.focus(),但这似乎没有任何作用。

【问题讨论】:

  • 这正是the Notification API 的用途。
  • Good old alert 为某些浏览器做过一次。幸好没有了。
  • @kapep 在 chrome 中仍然如此。
  • 正如我在问题中所说的那样,我已经在使用通知 API...我一直在寻找的只是让任务栏项目闪烁的东西

标签: javascript minimized web-technologies html5-notifications


【解决方案1】:

现代浏览器实现Notification API。这是一个例子:

(function() {
  var button, output;

  function scheduleNotifyTheUser() {
    output.innerHTML = "Stand by…";
    setTimeout(notifyTheUser, 1000);
  }
  
  function notifyTheUser() {
    output.innerHTML = "Ding! You have a message!";
    new Notification("Something Happened", {
      body: "You should totally check this out!"
    });
  }
  
  document.addEventListener("DOMContentLoaded", function() {
    button = document.getElementById("gobtn");
    output = document.getElementById("output");
    output.innerHTML = "Requesting permission…";
    Notification.requestPermission(function(permission) {
      if (permission !== "granted") {
        output.innerHTML = "Notifications disabled.";
        return;
      }
      button.addEventListener("click", scheduleNotifyTheUser);
      output.innerHTML = "Ready.";
    });
  });
})();
<button id="gobtn">Make something happen in one second from now</button>
<div id="output"></div>

【讨论】:

  • 由于某种原因,当窗口最小化时,在显示通知之前会有 7-8 秒的延迟(我使用的是 chrome)。知道为什么吗?
  • 很可能是因为窗口不在焦点上。浏览器很好,并且当页面不在最前沿(正在使用)时会减慢页面速度,因此执行操作的机会将减少。至少我认为是这种情况,因为我不知道您的代码。如果您需要来自操作系统的更多资源,那么您需要一个后台网页,然后您正在考虑制作一个原生桌面应用程序。如果您想将 HTML/JavaScript 用于桌面应用程序,Node-webkit 可能是一个选项。
  • 是的,这些是桌面通知...非常好,只是我已经在使用这些了。我要求的不是这个。只会使任务栏项目反弹或闪烁的东西。估计什么都没有
猜你喜欢
  • 1970-01-01
  • 2017-07-27
  • 2013-04-17
  • 1970-01-01
  • 1970-01-01
  • 2011-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多