HTML5 web通知API介绍
2015/04/17 · HTML5 · 2
评论 ·
web通知
本文由 伯乐在线 –
ElvisKang
翻译,周进林
校稿。未经许可,禁止转发!
英文出处:www.sevensignature.com。欢迎参预翻译组。
在利用网页版Gmail的时候,每当收到新邮件,显示器的右下方都会弹出相应的提醒框。借助HTML5提供的Notification
API,大家也得以轻松达成如此的法力。
点评:HTML5的强劲之一就是同意web程序提请一些暂时或者永久的空中(Quota)在这边可以开展数据的储存甚至文件的操作
利用HTML5的Notification API制作web通告的教程,html5notification
那篇小说紧要介绍了应用HTML5的Notification
API制作web通告的教程,示例包含须要使用到的连带CSS以及Javascript代码,要求的朋友可以参见下
在选拔网页版Gmail的时候,每当收到新邮件,屏幕的右下方都会弹出相应的提醒框。借助HTML5提供的Notification
API,我们也可以轻松达成那样的成效。
担保浏览器接济
设若你在特定版本的浏览器上拓展开发,那么我提出你先到 caniuse
查看浏览器对Notification
API的支撑情状,避免你将难得时间浪费在了一个无法使用的API上。
什么样初始
JavaScript Code复制内容到剪贴板
- var notification=new Notification(‘Notification Title’,{
- body:’Your Message’
- });
地点的代码构造了一个简陋的布告栏。构造函数的第二个参数设定了布告栏的标题,而首个参数则是一个option
对象,该对象可设置以下属性:
- body :设置布告栏的正文内容。
dir
:定义文告栏文本的显得方向,可设为auto(自动)、ltr(从左到右)、rtl(从右到左)。
lang
:申明公告栏内文本所使用的语种。(译注:该属性的值必须属于BCP 47
language tag。)
API制作web通知的教程,API的使用介绍。 tag:为布告栏分配一个ID值,便于寻找、替换或移除通告栏。
icon :设置作为通告栏icon的图样的URL
得到权力
在体现布告栏此前需向用户申请权限,唯有用户同意,布告栏才可现身在显示屏中。对权力申请的处理将有以下重返值:
-
default:用户处理结果未知,因而浏览器将视为用户拒绝弹出文告栏。(“浏览器:你没需要关照,我就不打招呼你了”)
denied:用户拒绝弹出通知栏。(“用户:从自己的显示器里滚开”)
granted:用户同意弹出公告栏。(“用户:欢迎!我很欢喜可以运用那么些布告作用”)
JavaScript Code复制内容到剪贴板
- Notification.requestPermission(function(permission){
- //display notification here making use of constructor
- });
用HTML成立一个按钮
XML/HTML Code复制内容到剪贴板
- <button id=”button”>Read your notification</button>
并非遗忘了CSS
CSS Code复制内容到剪贴板
- #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 Code复制内容到剪贴板
- 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);
- });
- });
- });
从那段代码可以看看,若是浏览器不帮助Notification
API,在点击按钮时将会油然则生警示“兄弟,很对不起。你的浏览器并不能够很好地支撑布告效能”(Sorry
bro, your browser is not good enough to display
notification)。否则,在取得了用户的同意之后,大家自制的布告栏便得以出现在显示器中间啦。
缘何要让用户手动关闭公告栏?
对于这一个难点,大家得以凭借set提姆eout函数设置一个年华距离,使布告栏能定时关闭。
JavaScript Code复制内容到剪贴板
- 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。
API制作web通告的学科,html5notification
那篇小说主要介绍了采纳HTML5的Notification
API制作web公告的课程,示例包蕴需求采用到…
API是什么?
API中文翻译过来的趣味为:应用程序接口。上边谈谈自己的精通:
俺们把程序想象成一个房子,程序提供的效应对应房间,而API就是房间的门。
门上有三个按钮,对应参数;门上有牌子,上面写明打开门之后会看出的情景。开门的动作对应调用API;显示的风貌对应重返的结果。而房间不止一个,当然门也会有几个。
表明:我们不用去精晓,房间如故门是何许建造的,就如开发者不用去精晓函数内部代码的落到实处。大家要求做的,仅仅是按下按钮,看牌子以及打开门。
保障浏览器帮衬
万一你在特定版本的浏览器上进行付出,那么我提出您先到
caniuse
查看浏览器对Notification
API的支撑景况,避免你将难得时间浪费在了一个无法使用的API上。
HTML5的无敌之一就是允许web程序提请一些暂时或者永久的上空(Quota)在此地可以展开
数据的储存甚至文件的操作。
web API
弄通晓了API,web API就好精晓了!
根据http协议的API,相当于把门放在web页面上。
怎么样开端
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
FileSystem提供了文件夹和文件的创制、移动、删除等操作,大大便利了数量的当地处理,
而且所有的数目都是在沙盒(sandboxed)中,分化的web程序不可以互相走访,那就有限支撑了数据
的全部和天水。
API框架
其一更好通晓了!
制作门的模型而已。
得到权力
在浮现布告栏此前需向用户申请权限,唯有用户同意,公告栏才可出现在屏幕中。对权力申请的处理将有以下重返值:
- 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)。否则,在收获了用户的允许之后,大家自制的布告栏便得以出现在显示屏中间啦。
怎么要让用户手动关闭公告栏?
对此那几个标题,我们可以依靠set提姆eout函数设置一个年华间隔,使文告栏能定时关闭。
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
在CatWrite项目中,运用了HTML5的这么些特点开展数据的仓储,非凡便民,只是近年来的话唯有Chrome浏览器对FileSystem API辅助的比较好,所以不得不运行在Chrome浏览器中。
在CodePen上查看demo
您可以在CodePen上收看由Prakash
(@imprakash)编写的demo。
赞 收藏 2
评论
在成就这一个成效的时候,查阅了诸多材料,有一对素材是一年前的,但是随着浏览器版本的
变化,一些代码已经老化,在那边一一总计和整理。那里只列举了档次中用到的API,算是
对达成效能的四次梳理。
关于小编:ElvisKang
QQ:285273704新浪:@康恺暄邮箱:elviskang@foxmail.com
个人主页 ·
我的稿子 ·
10
申请空间 为了拓展数据的积存,必必要向浏览器进行申请,假若是永远存储还会向用户展开询问,只有同意后才会继续执行。
率先必必要申明想要的权力。
代码如下:
window.requestFileSystem = window.requestFileSystem ||
window.webkitRequestFileSystem; //文件系统请求标识
window.resolveLocalFileSystemURL = window.resolveLocalFileSystemURL ||
window.webkitResolveLocalFileSystemURL; //按照URL取得文件的读取权限