HTML5 web通知API介绍

HTML5 web通知API介绍

2015/04/17 · HTML5 · 2
评论 ·
web通知

本文由 伯乐在线 –
ElvisKang
翻译,周进林
校稿。未经许可,禁止转发!
俄语出处:www.sevensignature.com。应接参预翻译组。

图片 1

在应用网页版Gmail的时候,每当收到新邮件,荧屏的右下方都会弹出相应的提醒框。借助HTML5提供的Notification
API,我们也得以轻巧达成那样的效果与利益。

管教浏览器帮衬

假设您在一定版本的浏览器上拓张开采,那么本人建议你先到
caniuse
查看浏览器对Notification
API的帮衬意况,制止你将难得时间浪费在了三个不恐怕选拔的API上。

如何开始

JavaScript

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

1
2
3
var notification=new Notification(‘Notification Title’,{
    body:’Your Message’
});

下面的代码构造了一个简陋的文告栏。构造函数的第二个参数设定了布告栏的标题,而第三个参数则是贰个option
对象,该指标可安装以下属性:

  • body :设置公告栏的正文内容。
  • dir
    :定义通告栏文本的突显方向,可设为auto(自动)、ltr(从左到右)、rtl(从右到左)。
  • lang
    :注解文告栏内文本所使用的语种。(译注:该属性的值必得属于BCP 47
    language tag。)
  • tag:为布告栏分配三个ID值,便于寻找、替换或移除文告栏。
  • icon :设置作为通告栏icon的图片的UKugaL

获得权力

在映现文告栏在此之前需向顾客申请权限,只有客商同意,通知栏才可现身在荧屏中。对权力申请的拍卖将有以下重回值:

  • default:客户管理结果未知,由此浏览器将视为客户拒绝弹出公告栏。(“浏览器:你没必要文告,笔者就不公告你了”)
  • denied:客户拒绝弹出通告栏。(“顾客:从自己的显示屏里滚开”)
  • granted:顾客同意弹出文告栏。(“顾客:应接!小编很开心能够利用那些文告成效”)
JavaScript

Notification.requestPermission(function(permission){ //display
notification here making use of constructor });

<table>
<colgroup>
<col style="width: 50%" />
<col style="width: 50%" />
</colgroup>
<tbody>
<tr class="odd">
<td><div class="crayon-nums-content" style="font-size: 13px !important; line-height: 15px !important;">
<div class="crayon-num" data-line="crayon-5b8f063d76a49818832322-1">
1
</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-5b8f063d76a49818832322-2">
2
</div>
<div class="crayon-num" data-line="crayon-5b8f063d76a49818832322-3">
3
</div>
</div></td>
<td><div class="crayon-pre" style="font-size: 13px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
<div id="crayon-5b8f063d76a49818832322-1" class="crayon-line">
Notification.requestPermission(function(permission){
</div>
<div id="crayon-5b8f063d76a49818832322-2" class="crayon-line crayon-striped-line">
//display notification here making use of constructor
</div>
<div id="crayon-5b8f063d76a49818832322-3" class="crayon-line">
});
</div>
</div></td>
</tr>
</tbody>
</table>

用HTML制造二个开关

XHTML

<button id=”button”>Read your notification</button>

1
<button id="button">Read your notification</button>

并不是忘记了CSS

CSS

#button{ font-size:1.1rem; width:200px; height:60px; border:2px solid
#df7813; border-radius:20px/50px; background:#fff; color:#df7813; }
#button:hover{ background:#df7813; color:#fff; transition:0.4s ease;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#button{
  font-size:1.1rem;
  width:200px;
  height:60px;
  border:2px solid #df7813;
  border-radius:20px/50px;
  background:#fff;
  color:#df7813;
}
#button:hover{
  background:#df7813;
  color:#fff;
  transition:0.4s ease;
}

全部的Javascript代码如下:

JavaScript

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:”,
dir:’auto’ }; var notification = new Notification(“Here I am!”,config);
}); }); });

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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);
            });
        });
    });

从这段代码能够看看,假设浏览器不协理Notification API,在点击开关时将会油不过生警示“兄弟,很对不起。你的浏览器并不能够很好地帮助公告作用”(Sorry
bro, your browser is not good enough to display
notification)。不然,在获得了客户的同意之后,大家自制的文告栏便足以出今后显示器中间啦。

为啥要让客商手动关闭通告栏?

对此这几个主题材料,大家能够借助setTimeout函数设置二个光阴距离,使文告栏能定期关闭。

JavaScript

var config = { body:’Today too many guys got eyes on me, you did the
same thing. Thanks’, icon:’icon.png’, dir:’auto’ } var notification =
new Notification(“Here I am!”,config); setTimeout(function(){
notification.close(); //closes the notification },5000);

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

该说的事物就那么些了。就算您意犹未尽,希望尤其历历在目地打听Notification
API,能够阅读以下的页面:

  • MDN
  • Paul lund’s tutorial on notification
    API

在CodePen上查看demo

您能够在CodePen上见到由Prakash
(@imprakash)编写的demo。

赞 收藏 2
评论

关于小编:ElvisKang

图片 2

QQ:285273704微博:@康恺暄邮箱:elviskang@foxmail.com
个人主页 ·
作者的文章 ·
10

图片 3

相关文章