var notification=new Notification(‘Notification Title',{
body:'Your Message'
});

上面的代码构造了一个简陋的通知栏。构造函数的第一个参数设定了通知栏的标题,而第二个参数则是一个option 对象,该对象可设置以下属性:

  • body :设置通知栏的正文内容。
        dir :定义通知栏文本的显示方向,可设为auto(自动)、ltr(从左到右)、rtl(从右到左)。
        lang :声明通知栏内文本所使用的语种。(译注:该属性的值必须属于BCP 47 language tag。)
        tag:为通知栏分配一个ID值,便于检索、替换或移除通知栏。
        icon :设置作为通知栏icon的图片的URL

获取权限

在显示通知栏之前需向用户申请权限,只有用户允许,通知栏才可出现在屏幕中。对权限申请的处理将有以下返回值:

    • default:用户处理结果未知,因此浏览器将视为用户拒绝弹出通知栏。(“浏览器:你没要求通知,我就不通知你了”)
          denied:用户拒绝弹出通知栏。(“用户:从我的屏幕里滚开”)
          granted:用户允许弹出通知栏。(“用户:欢迎!我很高兴能够使用这个通知功能”)
    • Notification.requestPermission(function(permission){
      //display notification here making use of constructor
      });
      <button id="button">Read your notification</button>  
      #button{
      font-size:.1rem;
      width:200px;
      height:60px;
      border:2px solid #df7813;
      border-radius:20px/50px;
      background:#fff;
      color:#df7813;
      }
      #button:hover{
      background:#df7813;
      color:#fff;
      transition:.4s ease;
      }
      document.addEventListener('DOMContentLoaded',function(){
      document.getElementById('button').addEventListener('click',function(){
      if(! ('Notification' in window) ){
      alert('Sorry bro, your browser is not good enough to display notification');
      return;
      }
      Notification.requestPermission(function(permission){
      var config = {
      body:'Thanks for clicking that button. Hope you liked.',
      icon:'https://cdn2.iconfinder.com/data/icons/ios-7-style-metro-ui-icons/512/MetroUI_HTML5.png',
      dir:'auto'
      };
      var notification = new Notification("Here I am!",config);
      });
      });
      });

      关闭通知的实例方法:

      var n = new Notification(theTitle,options);
      setTimeout(n.close.bind(n), );
    • 参考:http://www.jb51.net/html5/323493.html
    • https://codepen.io/imprakash/pen/ZYLayY

最新文章

  1. 关于解决python线上问题的几种有效技术
  2. Codeforces Round #384 (Div. 2) B. Chloe and the sequence(规律题)
  3. Python:python中math模块中提供的基本数学函数
  4. ActiveMQ_Topic队列(三)
  5. Java-小数点控制
  6. Capturing Audio &amp; Video in HTML5
  7. 使用solr的函数查询,并获取tf*idf值
  8. Python学习 之 对内存的使用(浅拷贝和深拷贝)
  9. CentOS下安装配置Cacti
  10. 大端模式&amp;小端模式、主机序&amp;网络序、入栈地址高低问题
  11. 微信移动支付V3开发详细教程服务端采用.net mvc webapi(C#)
  12. Ubuntu-1604-LTS在虚拟机设置分辨率
  13. HGOI 20190407 Typing Competition Round #1 出题记
  14. shell脚本介绍
 shell脚本结构和执行
date命令用法
 shell脚本中的变量
  15. .Net Framework4.5.2 源码命名空间简析
  16. Java对MySQL数据库进行连接、查询和修改(转)
  17. jquery判断浏览器的内核
  18. 层层递进——宽度优先搜索(BFS)
  19. CentOs - 使用ssh key远程登录
  20. linux下nginx tomcat集群

热门文章

  1. JavaScript学习10:动态载入脚本和样式
  2. Google Chrome 39.0.2171.71 正式发布
  3. Unity 国际化 多语言设置
  4. Oracle EBS R12 - 怎样更改SYSADMIN的password
  5. 进度对话框 ProgressDialog 用法总结
  6. 小课堂week18 编程范式巡礼第三季 谈谈依赖反转
  7. Pairwise
  8. sh: 1: node: Permission denied
  9. scala lambda 小括号与大括号
  10. DataColumn