html5谷歌浏览器/火狐浏览器的桌面通知
2014-4-30 13:25 Wednesday  

分类: Javascript 标签: http spl window else function 评论(43) 浏览(34058)

先看一下Discuz!中的谷歌js代码

function Html5notification() {
	var h5n = new Object();

	h5n.issupport = function() {
		var is = !!window.webkitNotifications;
		if(is) {
			if(window.webkitNotifications.checkPermission() > 0) {
				window.onclick = function() {
					window.webkitNotifications.requestPermission();
				}
			}
		}
		return is;
	};

	h5n.shownotification = function(replaceid, url, imgurl, subject, message) {
		if(window.webkitNotifications.checkPermission() > 0) {
			window.webkitNotifications.requestPermission();
		} else {
			var notify = window.webkitNotifications.createNotification(imgurl, subject, message);
			notify.replaceId = replaceid;
			notify.onclick = function() {
				window.focus();
				window.location.href = url;
				notify.cancel();
			};
			notify.show();
		}
	};

	return h5n;
}

如何定时关闭呢,

    if (window.webkitNotifications) { //是否存在api
         if (window.webkitNotifications.checkPermission() == 0) { //是否有权限了
               var pop =  window.webkitNotifications.createNotification('http://i8.baidu.com/it/u=2683190826,2755071058&fm=96&s=A8208C18533E458852E8E5D2030030B3', '来了', '你知道吗');
               pop.ondisplay = function(event) {
                   setTimeout(function() {
                       event.currentTarget.cancel();//关闭
                   }, 3000);
               }
               pop.show(); //显示
           } else {
               window.webkitNotifications.requestPermission();//激活是否显示通知对话条
               return;
           }
     }

注意,一定要用户点击才能激发权限判断显示通知的

下面是火狐浏览器的

 

function notifyMe() {
  // Let's check if the browser supports notifications
  if (!("Notification" in window)) {
    alert("This browser does not support desktop notification");
  }

  // Let's check if the user is okay to get some notification
  else if (Notification.permission === "granted") {
    // If it's okay let's create a notification
    var notification = new Notification("Hi there!");
  }

  // Otherwise, we need to ask the user for permission
  // Note, Chrome does not implement the permission static property
  // So we have to check for NOT 'denied' instead of 'default'
  else if (Notification.permission !== 'denied') {
    Notification.requestPermission(function (permission) {

      // Whatever the user answers, we make sure we store the information
      if(!('permission' in Notification)) {
        Notification.permission = permission;
      }

      // If the user is okay, let's create a notification
      if (permission === "granted") {
        var notification = new Notification("Hi there!");
      }
    });
  }

  // At last, if the user already denied any notification, and you
  // want to be respectful there is no need to bother him any more.
}
//来源:https://developer.mozilla.org/en/docs/Web/API/notification

 

 

 

 

 

 

+1 12

留下你的看法: