十五年测试老手,长期负责WEB\APP 项目测试,目前主要负责团队管理工作。

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软件测试网UQj|%F)S

桌面提醒功能由 window.webkitNotifications 对象实现。51Testing软件测试网7uO TdM

window.webkitNotifications 对象没有属性,有四个方法:51Testing软件测试网^U/xGb'U f,^,Q`.j

1.requestPermission()

sp Ut1EF"]6[0

  该方法用于向用户申请消息提醒权限,如果当前没有开放该权限,浏览器将弹出授权界面,用户授权后,在对象内部产生一个状态值(一个0、1或 2 的整数):51Testing软件测试网%U]m u7O-x+s*H/?

       0:表示用户同意消息提醒,只在该状态下可以使用信息提醒功能;

;md6a,O6W0}lmC(I0

       1:表示默认状态,用户既未拒绝,也未同意;51Testing软件测试网 h3Y7f7Z JaE

       2:表示用户拒绝消息提醒。51Testing软件测试网PB%N)EHp H7R

2.checkPermission()

/f?9X[ _H3`#u0

  这个方法用于获取 requestPermission() 申请到的权限的状态值。

FrMPE:o'P0

51Testing软件测试网5n7w6LHF j)px

Py I-Nb LZH:_:x0

3.createNotification()51Testing软件测试网9v3D#o$Q&l4K5^o8w @y

  这个方法以纯消息的方式创建提醒消息,它接受三个字符串参数:

#a`.~&e#J7Pjf0

        iconURL:在消息中显示的图标地址,51Testing软件测试网/JE1l&`4R0H1ogt

        title:消息的标题,

)No"M] f5Gm;_"d0

        body:消息主体文本内容

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

  1. dir:""   51Testing软件测试网 i?6_7U*Qc~
    51Testing软件测试网5Y+H` vYaI ^

  2. onclick:null51Testing软件测试网tb8q,B~*T{qn

  3. onclose:null

    j-HJtm3DB\)P0
  4. ondisplay:function (event) {51Testing软件测试网'q^/vqW

  5. onerror:null51Testing软件测试网dZ2Ge3w

  6. onshow:null

    h$SL.DY7P"W0
  7. replaceId:""51Testing软件测试网"_)U*z8B/Fa}

  8. tag:""

    )V)ap3gU$mE9M;}0
  9. __proto__:Notification

    !n,` g&lZ3vs0
  10. addEventListener:function addEventListener() { [native code] }

    "f`:oq EZ2F.S_X0
  11. cancel:function cancel() { [native code] }

    w)u7W&I`0
  12. close:function close() { [native code] }

    *[-N}ya8E#A0
  13. constructor:function Notification() { [native code] }

    .Y~ \#uL-p0H(G0
  14. dispatchEvent:function dispatchEvent() { [native code] }51Testing软件测试网J;J0t:xWB [

  15. removeEventListener:function removeEventListener() { [native code] }51Testing软件测试网a;W ql0F L

  16. show:function show() { [native code] }

    Tg)q Yt F0
  17. __proto__:Object
    5}_G QI8qP F051Testing软件测试网J"d%`/lw;Pi&Vm/c

51Testing软件测试网xp-d9AXjIz

5t4h0IM7jN0

  dir:设置消息的排列方向,可取值为“auto”(自动), “ltr”(left to right), “rtl”(right to left)。

H#ng:Uy i/Q;vpW0

  tag:为消息添加标签名。如果设置此属性,当有新消息提醒时,标签相同的消息只显示在同一个消息框,后一个消息框会替换先前一个,否则出现多个消息提示框,但是最多值显示3个消息框,超过3个,后继消息通知会被阻塞。51Testing软件测试网 q8q0uK"TbI6mgn`

  onshow:当消息框显示的时候触发该事件;

%n3I;A.fWgN"q$T0

  onclick: 当点击消息框的时候触发该事件;

6kf@K?(`DY4n0

  onclose:当消息关闭的时候触发该事件;51Testing软件测试网aH8Bo/o5{ I}X.S

  onerror:当出现错误的时候触发该事件;51Testing软件测试网0]A,?6r:c

方法:

(VVquZ O.})D0

  addEventListener && removeEventListener:常规的添加和移除事件方法;51Testing软件测试网g,}e;i2T6Qm:Md{

  show:显示消息提醒框;51Testing软件测试网6EAgj#K { o1I$QT

  close:关闭消息提醒框;51Testing软件测试网+s%o)CE2^

  cancel:关闭消息提醒框(似乎和 close一样);51Testing软件测试网)~R(bZ W0]b |


sX rw9C#bB0

\4Qo:G Z2I-H0Y0

4.createHTMLNotification()

n!L[GH9J|g%R'E+_0

  该方法与 createNotification() 不同的是,他以HTML方式创建消息,接受一个参数: HTML 文件的URL,该方法同样返回 Notification对象。51Testing软件测试网 sQ"nAKT.x/l?)] c

51Testing软件测试网3l Y*u`Z M @
51Testing软件测试网|;~ OPNd6RN1m

51Testing软件测试网1F| g%gB,JL3XTw
51Testing软件测试网0Ig+YK|

一个实例:

'h3b)j$L(ES0

51Testing软件测试网ir)])n_.j P6nY%w:g

'e8j l FP*v0

<!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">  
U7d N&}mU051Testing软件测试网N%t7|Mi6m
        document.getElementById("trynotification").onclick = function(){51Testing软件测试网wLv C5_.^
            notify(Math.random());51Testing软件测试网$_3b'QKh#yE
        };
6T&a1`Wu2u1E051Testing软件测试网P%E,x$v e1f`l
        function notify(tab) {  51Testing软件测试网 ZGeXa7{3c
            if (!window.webkitNotifications) { 
bw y{ nn0                return false;
F_7x S4dW(F7p2V0            }
&D.o M.n y3vE7C%v0
KCL"g6K/A0            var permission = window.webkitNotifications.checkPermission();  51Testing软件测试网,td%m5|!u!{\
            if(permission!=0){                   51Testing软件测试网D:Gs z/a}:@
                window.webkitNotifications.requestPermission(); 51Testing软件测试网,H3c8_)o-A
                var requestTime = new Date();51Testing软件测试网5W.L P.e5{"s
                var waitTime = 5000;  51Testing软件测试网,i3V Fq Py
                var checkPerMiniSec = 100;            
5r(FwP'n,K0                setTimeout(function(){
-v0} `3kM%A$} L0                    permission = window.webkitNotifications.checkPermission();  51Testing软件测试网:Y:XEy3iQ]mI1ydb
                    if(permission==0){
u7_@4A"C)y6w$`6e0                        createNotification(tab);
9_/iuH+uzWFA7f0                    }else if(new Date()-requestTime<waitTime){51Testing软件测试网T;\;j?zJn5v+||
                        setTimeout(arguments.callee,checkPerMiniSec);51Testing软件测试网6F"]+o$s n1xP @
                    }51Testing软件测试网8d\ID3X,_G
                },checkPerMiniSec);51Testing软件测试网QR3t U1yU0c
            }else if(permission==0){
&pr)R.j)G0                createNotification(tab);51Testing软件测试网*R7DV F2o
            }   
tM:w,It d"fNB3j$T0        }  
'ha&Lw] ^:u051Testing软件测试网^'Ew0Z~oEy\&l
         function createNotification(tab){51Testing软件测试网z r]g'o
                var showSec = 10000;  51Testing软件测试网@ti'd2K
                var icon = "http://tech.baidu.com/resource/img/logo_news_137_46.png";  51Testing软件测试网s1V%fkl"c#g
                var title = "[" + new Date().toLocaleTimeString() + "] close after " + (showSec/1000) + " seconds";  
d;b UT9PT/N8O0                var body =  "hello world, i am webkitNotifications informations";        
/~ ?0F xDDd0
&r'C+|AH_C)Y8v%e0                var popup = window.webkitNotifications.createNotification(icon, title, body);  
4M5|[sV:S Q0                popup.tag = tab;
]'W;~moW&RC0                popup.ondisplay = function(event) {  51Testing软件测试网Gmb ow/E/j&S
                    setTimeout(function() { 
~o+H0S0r;}/`M7U0                        event.currentTarget.cancel();  
0IQ1\5Duh/f']2u0                    }, showSec);  
R8gS,_JL0                }  51Testing软件测试网+^1`lz Wx
                popup.show();
z J;CFdn%p0BQ0        }51Testing软件测试网D fnm.b8}4\y
51Testing软件测试网L!sO-Y\S
    </script>  
-cT'o l:[8x0
(\C*irS/D.A0c `0</body>  51Testing软件测试网F#f'r9s5Vu
</html>

:]nW)}-n q0

TAG: HTML5 html5

 

评分:0

我来说两句

Open Toolbar