如果同时发送多个桌面通知,Firefox不会显示桌面通知

时间:2015-05-26 14:55:09

标签: javascript jquery

我正在尝试为我的应用程序实现桌面通知。如果发送一个通知,它工作正常,但是当同时发送多个通知时,firefox不会显示任何通知。 Chrome无法解决此问题。

这是firefox无法实现的吗?我的印象是,通知选项中的标签用于堆叠通知。

代码:

function isNewNotificationSupported() {
  if (!window.Notification || !Notification.requestPermission) return false;
  if (Notification.permission == "granted") throw new Error("");
  try {
    new Notification("");
  } catch (e) {
    if (e.name == "TypeError") return false;
  }
  return true;
}

function notifyMe(aa, bb, cc, dd) {
  if (!("Notification" in window)) {
    //alert("This browser does not support desktop notification");
  } else if (Notification.permission === "granted") {
    if (!document.hasFocus() || cc == 1) {
      var options = {
        body: bb,
        icon: "",
        dir: "ltr",
        tag: dd
      };
      var notification = new Notification(aa, options);

      notification.onshow = function() {
        setTimeout(notification.close.bind(notification), 15000);
      };
    }
  } else if (Notification.permission !== "denied") {
    if (isNewNotificationSupported()) {
      Notification.requestPermission(function(permission) {
        if (!("permission" in Notification)) {
          Notification.permission = permission;
        }

        if (permission === "granted") {
          var options = {
            body: bb,
            icon: "",
            dir: "ltr",
            tag: dd
          };
          var notification = new Notification(aa, options);

          notification.onshow = function() {
            setTimeout(notification.close.bind(notification), 15000);
          };
        }
      });
    }
  }
}

notifyMe("New notification1","newtest","1","test1");
notifyMe("New notification2","newtest2","1","test2");

我在:

创建了一个jsfiddle

http://jsfiddle.net/1bm0wyvf/

更新:我想我现在通过将其中一个更改为:

来解决它
setTimeout(function() { notifyMe("Newnotification1","newtest","1","test1"); }, 200);

Firefox现在正确堆叠它们。

1 个答案:

答案 0 :(得分:0)

我正在使用以下功能,它在Firefox / Linux上的运行情况很好。

function notif(message){

 if (Notification.permission !== 'denied') {
    Notification.requestPermission(function (permission) {
      if (!('permission' in Notification)) {
        Notification.permission = permission
      }
      if (permission === "granted") {
          let notification = new Notification(message)
      }
    })
  }
}
notif("Hello world!!")

enter image description here