• 企业400电话
  • 微网小程序
  • AI电话机器人
  • 电商代运营
  • 全 部 栏 目

    企业400电话 网络优化推广 AI电话机器人 呼叫中心 网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    使用HTML5的Notification API制作web通知的教程

    在使用网页版Gmail的时候,每当收到新邮件,屏幕的右下方都会弹出相应的提示框。借助HTML5提供的Notification API,我们也可以轻松实现这样的功能。
    确保浏览器支持

    如果你在特定版本的浏览器上进行开发,那么我建议你先到 caniuse 查看浏览器对Notification API的支持情况,避免你将宝贵时间浪费在了一个无法使用的API上。
    如何开始
     

    JavaScript Code复制内容到剪贴板
    1. var notification=new Notification(‘Notification Title',{  
    2.     body:'Your Message'   
    3. });   
    4.   

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

    获取权限

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

    JavaScript Code复制内容到剪贴板
    1. Notification.requestPermission(function(permission){   
    2.     //display notification here making use of constructor   
    3.     });   
    4.   

    用HTML创建一个按钮
     

    XML/HTML Code复制内容到剪贴板
    1. <button id="button">Read your notification</button>  
    2.   

    不要忘记了CSS
     

    CSS Code复制内容到剪贴板
    1. #button{   
    2.   font-size:1.1rem;   
    3.   width:200px;   
    4.   height:60px;   
    5.   border:2px solid #df7813;   
    6.   border-radius:20px/50px;   
    7.   background:#fff;   
    8.   color:#df7813;   
    9. }   
    10. #button:hover{   
    11.   background:#df7813;   
    12.   color:#fff;   
    13.   transition:0.4s ease;   
    14. }   
    15.   

    全部的Javascript代码如下:
     

    JavaScript Code复制内容到剪贴板
    1. document.addEventListener('DOMContentLoaded',function(){   
    2.         document.getElementById('button').addEventListener('click',function(){   
    3.             if(! ('Notification' in window) ){   
    4.                 alert('Sorry bro, your browser is not good enough to display notification');   
    5.                 return;   
    6.             }       
    7.             Notification.requestPermission(function(permission){   
    8.                 var config = {   
    9.                               body:'Thanks for clicking that button. Hope you liked.',   
    10.                               icon:'https://cdn2.iconfinder.com/data/icons/ios-7-style-metro-ui-icons/512/MetroUI_HTML5.png',   
    11.                               dir:'auto'  
    12.                               };   
    13.                 var notification = new Notification("Here I am!",config);   
    14.             });   
    15.         });   
    16.     });   
    17.   

    从这段代码可以看出,如果浏览器不支持Notification API,在点击按钮时将会出现警告“兄弟,很抱歉。你的浏览器并不能很好地支持通知功能”(Sorry bro, your browser is not good enough to display notification)。否则,在获得了用户的允许之后,我们自制的通知栏便可以出现在屏幕当中啦。

    为什么要让用户手动关闭通知栏?

    对于这个问题,我们可以借助setTimeout函数设置一个时间间隔,使通知栏能定时关闭。
     

    JavaScript Code复制内容到剪贴板
    1. var config = {   
    2.                body:'Today too many guys got eyes on me, you did the same thing. Thanks',   
    3.                icon:'icon.png',   
    4.                dir:'auto'  
    5.              }   
    6. var notification = new Notification("Here I am!",config);   
    7. setTimeout(function(){   
    8.     notification.close(); //closes the notification   
    9. },5000);   
    10.   

    该说的东西就这些了。如果你意犹未尽,希望更加深入地了解Notification API,可以阅读以下的页面:

        MDN
        Paul lund’s tutorial on notification API

    在CodePen上查看demo

    你可以在CodePen上看到由Prakash (@imprakash)编写的demo。

    上一篇:利用HTML5中的Canvas绘制一张笑脸的教程
    下一篇:简单介绍HTML5中的文件导入
  • 相关文章
  • 

    © 2016-2020 巨人网络通讯

    时间:9:00-21:00 (节假日不休)

    地址:江苏信息产业基地11号楼四层

    《增值电信业务经营许可证》 苏B2-20120278

    使用HTML5的Notification API制作web通知的教程 使用,HTML5,的,Notification,