网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

如何实现html5桌面通知-创新互联

这篇文章主要讲解了“如何实现html5桌面通知”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“如何实现html5桌面通知”吧!

成都创新互联从2013年开始,是专业互联网技术服务公司,拥有项目网站建设、成都网站设计网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元右玉做网站,已为上家服务,为右玉各地企业和个人服务,联系电话:18980820575

html5桌面通知(Web Notifications)对于需要实现在新消息入线时,有桌面通知效果的情况下非常有用,在此简单介绍一下这个html5的新属性。

这里有个不错的demo:html5 web notification demo

从上面这个demo中 我们就可以获取所需要的基本核心代码,如下:

代码如下:


 
其中:Notification.requestPermission 这句代码的功能就是向用户请求权限允许

通过以上的例子,基本思路我们已经有了,首先加载文档时,就向用户请求权限,获取权限后以后都so easy了。

代码如下:

window.addEventListener('load', function () {
 // At first, let's check if we have permission for notification
 if (Notification && Notification.permission !== "granted") {
   Notification.requestPermission(function (status) {
     if (Notification.permission !== status) {
       Notification.permission = status;
     }
   });
 }
});

火狐下 验证是通过的,但是在chrome下总是出不来,后来发现这样一段话

代码如下:

Not a Bug, Feature.

Desktop Notifications can only be triggered via a user action.  Typing into the
JavaScript console has the same effect as raw javascript code embedded into the web
page (no user action).  Typing the javascript into the location bar, however,
represents a user-action (the user is intentionally visiting a javascript link to
enable notifications, probably for sites that tend to use href="javascript:" instead
of onclick="".

I'm pretty sure this is a non-issue.

原来在chrome下是必须要用户手动触发的,否则,chrome浏览器会无视这段的js

但是在我们网站里肯定不可能加一个按钮或者超链接来显式的让用户授权吧,好吧, 实际上这也不是个事情,我们可以在用户经常点的按钮上顺便处理下这个授权就好,在chrome下是一次授权终身有用。除非你进入设置把他禁了。

整合一下,代码如下:

代码如下:

function showMsgNotification(title, msg){
   var Notification = window.Notification || window.mozNotification || window.webkitNotification;
   
   if (Notification && Notification.permission === "granted") {
       var instance = new Notification(
               title, {
               body: msg,
               icon: "image_url"
           }
       );

       instance.onclick = function () {
           // Something to do
       };
       instance.onerror = function () {
           // Something to do
       };
       instance.onshow = function () {
           // Something to do
//          console.log(instance.close);
           setTimeout(instance.close, 3000);
       };
       instance.onclose = function () {
           // Something to do
       };
    }else if (Notification && Notification.permission !== "denied") {
         Notification.requestPermission(function (status) {
             if (Notification.permission !== status) {
               Notification.permission = status;
             }
             // If the user said okay
             if (status === "granted") {
                 var instance = new Notification(
                       title, {
                           body: msg,
                           icon: "image_url"
                       }
                   );

                   instance.onclick = function () {
                       // Something to do
                   };
                   instance.onerror = function () {
                       // Something to do
                   };
                   instance.onshow = function () {
                       // Something to do
                       setTimeout(instance.close, 3000);
                   };
                   instance.onclose = function () {
                       // Something to do
                   };
                   
             }else {
                 return false
             }
           });
     }else{
         return false;
     }

}

感谢各位的阅读,以上就是“如何实现html5桌面通知”的内容了,经过本文的学习后,相信大家对如何实现html5桌面通知这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联网站建设公司,,小编将为大家推送更多相关知识点的文章,欢迎关注!


网页名称:如何实现html5桌面通知-创新互联
链接地址:http://bjjierui.cn/article/cdedis.html

其他资讯