HTML5 web通知API介绍
2015/04/17 · HTML5 · 2
评论 ·
web通知
本文由 伯乐在线 –
ElvisKang
翻译,周进林
校稿。未经许可,禁止转载!
英文出处:www.sevensignature.com。欢迎加入翻译组。
在使用网页版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的图片的URL
获取权限
在显示通知栏之前需向用户申请权限,只有用户允许,通知栏才可出现在屏幕中。对权限申请的处理将有以下返回值:
- 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:’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);
}); }); });
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
QQ:285273704微博:@康恺暄邮箱:elviskang@foxmail.com
个人主页 ·
我的文章 ·
10