html5 桌面提醒:Notifycations
上一篇 / 下一篇 2013-05-05 11:02:46 / 个人分类:其它
文章来源
- 文章来源:【转载】
d+KR6MS0s ~ ^0 51Testing软件测试网$^;}b+[5?Q
HTML5中的桌面提醒(web notifications)可以在当前页面窗口弹出一个消息框,这个消息框是跨 Tab 窗口的,这在用户打开多个 tab 浏览网页时,提醒比较方便,容易让用户看到。目前实现这一功能的浏览器均以webkit为内核。51Testing软件测试网@+_;o!QL$r G|
该功能在 chrome 下需要以 http 方式打开网页才能启用。51Testing软件测试网koQd-`s8`
51Testing软件测试网(C
l#R p$r_!v3i9[a
51Testing软件测试网UQ j|%F)S
桌面提醒功能由 window.webkitNotifications 对象实现。51Testing软件测试网7u OTdM
window.webkitNotifications 对象没有属性,有四个方法:51Testing软件测试网^U/xGb'Uf,^,Q`.j
1.requestPermission()
sp Ut1EF"]6[0该方法用于向用户申请消息提醒权限,如果当前没有开放该权限,浏览器将弹出授权界面,用户授权后,在对象内部产生一个状态值(一个0、1或 2 的整数):51Testing软件测试网%U]m u7O-x+s*H/?
0:表示用户同意消息提醒,只在该状态下可以使用信息提醒功能;
;md6a,O6W0}lmC(I01:表示默认状态,用户既未拒绝,也未同意;51Testing软件测试网h3Y7f7ZJaE
2:表示用户拒绝消息提醒。51Testing软件测试网PB%N)EHpH7R
2.checkPermission()
/f?9X[_H3`#u0这个方法用于获取 requestPermission() 申请到的权限的状态值。
FrMP E:o'P051Testing软件测试网5n7w6LHFj)px
3.createNotification()51Testing软件测试网9v3D#o$Q&l4K5^o8w @y
这个方法以纯消息的方式创建提醒消息,它接受三个字符串参数:
#a`.~&e#J7Pjf0iconURL:在消息中显示的图标地址,51Testing软件测试网/JE1l&`4R0H1og t
title:消息的标题,
)No"M]f5Gm;_"d0body:消息主体文本内容
Zr\ Dw{(D:Y0该方法会返回一个 Notification对象,可以针对这个对象做更多的设置。51Testing软件测试网N:\7TS.g ]*ee b
51Testing软件测试网1TYny-S
51Testing软件测试网Hb!D)p7V,z
k
Notification 对象的属性与方法:51Testing软件测试网"`:Y@y:ch+z*y})R4a
51Testing软件测试网Bz^'`d$^
51Testing软件测试网K^5L A^]n
Dy
dir:"" 51Testing软件测试网 i?6_7U*Q c~
51Testing软件测试网5Y+H`vY aI^onclick:null51Testing软件测试网tb8q,B~*T{qn
onclose:null
j-HJtm3DB\)P0ondisplay:function (event) {51Testing软件测试网'q^/vqW
onerror:null51Testing软件测试网dZ2G e3w
onshow:null
h$SL.DY7P"W0replaceId:""51Testing软件测试网"_)U*z8B/Fa}
tag:""
)V)ap3g U$mE9M;}0__proto__:Notification
!n,` g&lZ3vs0addEventListener:function addEventListener() { [native code] }
"f`:oq EZ2F.S_X0cancel:function cancel() { [native code] }
w)u7W&I`0close:function close() { [native code] }
*[-N}ya8E#A0constructor:function Notification() { [native code] }
.Y~ \#uL-p0H(G0dispatchEvent:function dispatchEvent() { [native code] }51Testing软件测试网J;J0t:xWB [
removeEventListener:function removeEventListener() { [native code] }51Testing软件测试网a;W ql0F L
show:function show() { [native code] }
Tg)q YtF0__proto__:Object
5}_G Q I8qPF051Testing软件测试网J"d%`/lw;Pi&Vm/c
51Testing软件测试网xp-d9AXjIz
dir:设置消息的排列方向,可取值为“auto”(自动), “ltr”(left to right), “rtl”(right to left)。
H#ng:Uy i/Q;vpW0tag:为消息添加标签名。如果设置此属性,当有新消息提醒时,标签相同的消息只显示在同一个消息框,后一个消息框会替换先前一个,否则出现多个消息提示框,但是最多值显示3个消息框,超过3个,后继消息通知会被阻塞。51Testing软件测试网q8q0u K"TbI6mgn`
onshow:当消息框显示的时候触发该事件;
%n3I;A.fW gN"q$T0onclick: 当点击消息框的时候触发该事件;
6kf@K?(`DY4n0onclose:当消息关闭的时候触发该事件;51Testing软件测试网aH8Bo/o5{I}X.S
onerror:当出现错误的时候触发该事件;51Testing软件测试网0] A,?6r:c
方法:
(VVquZ O.})D0addEventListener && removeEventListener:常规的添加和移除事件方法;51Testing软件测试网g,}e;i2T6Qm:Md{
show:显示消息提醒框;51Testing软件测试网6EAgj#K {o1I$QT
close:关闭消息提醒框;51Testing软件测试网+s%o)CE2^
cancel:关闭消息提醒框(似乎和 close一样);51Testing软件测试网)~ R(bZW0]b|
sXrw9C#bB0
4.createHTMLNotification()
n!L[GH9J|g%R'E+_0该方法与 createNotification() 不同的是,他以HTML方式创建消息,接受一个参数: HTML 文件的URL,该方法同样返回 Notification对象。51Testing软件测试网 sQ"nAKT.x/l?)]c
51Testing软件测试网3lY*u`Z
M@
51Testing软件测试网 |;~
OPNd6RN1m
51Testing软件测试网1F|
g%gB,JL3XTw
51Testing软件测试网0Ig+YK|
一个实例:
'h3b)j$L(ES051Testing软件测试网ir)])n_.jP6nY%w:g
<!DOCTYPE HTML>
1\ nOn8a0<html> 51Testing软件测试网`q:hb,\;FpL,d
<head>
'H2Z-tE.b(T0 <title>notifications in HTML5</title>
"mU{LxX0</head>
U+F.}4E"@
Y#\Gn0<body> 51Testing软件测试网lG0e*m A*l{
<form> 51Testing软件测试网'~"o~I$U
<input id="trynotification" type="button" value="Send notification" />
Q1F6O x/__-t0 </form>
.z6O`L/{jLE051Testing软件测试网n'Vs8~@8kq0])R
<script type="text/javascript">