Web Notifications

用过QQ、Gtalk之类的同学,应该都被它的消息提醒所骚扰过。其实这里就要谈谈这玩意,对于桌面应用程序来说,这应该算不了什么大不了的;不过这相同的技术移植到另一个平台上,如Web应用上来说,就没那么简单了,这么W3C还没把它定案呢,各大浏览器商也支持不一。

今天正好手头没项目,就试玩了下Web Notifications;对,就是传说中的Web通知也可以简单说成消息提醒,就它的表现形式在W3C定义来说还是相当丰富的。下面我们就先来简单了解下官方的资料说明(以下资料大多翻译自W3C或浏览器官方网站,大可放心其可靠性。不过里面的一些专业词汇没有翻译),最后在看个简单的例子。

定义

Ambient notification:不需要用户任何操作自动弹出和消失的消息窗口;

Interactive notification:用户可以通过操作与应用传递信息的消息窗口;

Persistent notification:除非用户主动释放它,不然会一直显示的一种消息窗口;

Notification platform:桌面消息窗口,脱离UA的消息平台。如MacOS中的Growl、Linux中的NotifyOSD和Windows下的notification API;

Simple notification:由一个图标、一行或二行文本组成的一种简单消息提醒;

Web notification:根本Web应用程序决定的一种消息提醒,如HTML、SVG;

这里分享的就是第5种:Simple notification,我们可以把它定义成“简单消息提醒”,追求原版的可以移步:Web Notifications

例子

因为这份文档还没定案,所以就像上面提到的,目前的几大浏览器还支持不一,经过查找相关资料及测试,目前还只弄了个Chrome版本的,因此文章会不断地更新。

Chrome

前提条件是先把Google Chrome更新到10.0以上版本; 并把允许弹出桌面消息的功能打开(“Under the Hood" -> "Privacy" -> "Content settings" -> "Notifications",选择"Allow all sites to show desktop notifications".)

1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <meta charset="utf-8" />
 5 <title>Web Notifications</title>
 6 </head>
 7 
 8 <body>
 9 </body>
10 <script type="text/javascript">
11 var myNotifications = window.webkitNotifications;
12 if(myNotifications.checkPermission() === 1) {
13     myNotifications.requestPermission();
14 } else if (myNotifications.checkPermission() === 0) {
15     var notification = myNotifications.createNotification(‘images/message.jpg‘, ‘New Message Received‘, ‘The html5 has send you an email.‘);
16     notification.ondisplay = function() { setTimeout(‘notification.cancel()‘, 5000); }
17     notification.show();
18 } else {  
19     alert(‘Permission has been denied.‘);
20 }
21 </script>

在浏览器中打开此页面,就会看到桌面的右下角弹出一个持续5秒钟的消息窗口。如下图:

Chrome提供了对此窗口的简单管理功能,当你单击“功能管理”图标时,会发现有“禁止桌面弹出消息窗口”、“设置”(跳到上面提到的Notifications管理)、“管理此窗口显示位置”。

这个例子中会出现一种如下的情况:如果用户根本就没有开启允许弹出消息窗口功能,怎么办?也许你已经注意到了代码中的"myNotifications.requestPermission()",对这句代码的功能就是向用户请求权限允许的。现在我们就来试下把浏览器的"Notifications"的权限设置为"Ask me when a site wants to show desktop notifications(recommended)" 后再刷新此窗口,发现根本没弹出窗口,而且也没有弹出向用户请求权限的窗口,这到底是什么原因呢?(请看Issue 31736),发现第13条回复是这样的: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.

翻译:这不是个Bug,桌面消息只能通过用户来触发,在Javascript运行窗口、地址运行及直接在Javascript中运行都不是用户的主动行为 ,解决方法就是让用户来触发相关代码,如:通过超链接的方式,你需要做的就是把链接的href或onclick属性指向到你的代码片段即可。

既然如此,现将上面的代码改成如下:

1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <meta charset="utf-8" />
 5 <title>The Essential Guide to HTML5</title>
 6 </head>
 7 
 8 <body>
 9 <a href="javascript:popNotifications();">点击我</a>
10 </body>
11 <script type="text/javascript">
12 function popNotifications() {
13     var myNotifications = window.webkitNotifications;
14     if(myNotifications.checkPermission() === 1) {
15         myNotifications.requestPermission();
16     } else if (myNotifications.checkPermission() === 0) {
17         var notification = myNotifications.createNotification(‘images/list1.jpg‘, ‘New Message Received‘, ‘The html5 has send you an email.‘);
18         notification.ondisplay = function() { setTimeout(‘notification.cancel()‘, 5000); }
19         notification.show();
20     } else {  
21         alert(‘Permission has been denied.‘);
22     }
23 }
24 </script>
25 </html>

现在再来运行新的页面,当单击“点击我”后,出现请求权限的提示,如图:

点击“Allow”后,再次单击“点击我”,发现消息窗口出来了,这不正是我们需要的效果嘛,哈哈,已经完全成功了。不过我们还是值得再回头看下,权限允许后浏览器的设置,如下图:

浏览器已经将我们的网站添加到Notifications白名单中了(红色标记处),也就是说以后所有来自此网站的桌面消息窗口都不再需要请求权限直接通过了。

PS:其它浏览器版本会在以后不断更新进来。

时间: 2024-07-28 20:49:50

Web Notifications的相关文章

html5 Web Notifications

最近做的一个仿微信网页版的站点,有一个新需求, 需要实现在新消息入线时,有桌面通知的效果,所以最近就稍微了解一下这个html5的新属性. 这边有个不错的demo:html5 web notification demo 从上面这个demo中 我们就可以获取所需要的基本核心代码,如下: <script> var Notification = window.Notification || window.mozNotification || window.webkitNotification; Not

Progressive Web App

下一代 Web 应用? 近年来,Web 应用在整个软件与互联网行业承载的责任越来越重,软件复杂度和维护成本越来越高,Web 技术,尤其是 Web 客户端技术,迎来了爆发式的发展. 包括但不限于基于 Node.js 的前端工程化方案:诸如 Webpack.Rollup 这样的打包工具:Babel.PostCSS 这样的转译工具:TypeScript.Elm 这样转译至 JavaScript 的编程语言:React.Angular.Vue 这样面向现代 Web 应用需求的前端框架及其生态,也涌现出了

JavaScript资源大全

目录 前端MVC 框架和库 包管理器 加载器 打包工具 测试框架 框架 断言 覆盖率 运行器 QA 工具 基于 Node 的 CMS 框架 模板引擎 数据可视化 编辑器 UI 输入 日历 选择 文件上传 其它 提示 模态框和弹出框 滚动 菜单 表格/栅格 框架 手势 地图 视频/音频 动画 图片处理 ECMAScript 6 软件开发工具包(SDK) 利器 前端MVC 框架和库 angular.js:为网络应用增强 HTML.官网 aurelia:一个适用于移动设备.桌面电脑和 web 的客户端

前端组件库

//来源:http://www.cnblogs.com/liuzhibin/p/5944821.html 0. 前端自动化(Workflow) 前端构建工具 Yeoman – a set of tools for automating development workflow gulp – The streaming build system grunt – the JavaScript Task Runner F.I.S – 前端集成解决方案 前端模块管理器 Bower – A package

JavaScript 资源大全中文版

包管理器 管理着 javascript 库,并提供读取和打包它们的工具. npm:npm 是 javascript 的包管理器.官网 Bower:一个 web 应用的包管理器.官网 component:能构建更好 web 应用的客户端包管理器.官网 spm:全新的静态包管理器.官网 jam:一个专注于浏览器端和兼容 RequireJS 的包管理器.官网 jspm:流畅的浏览器包管理器.官网 Ender:没有库文件的程序库.官网 volo:以项目模板.添加依赖项与自动化生成的方式创建前端项目.官网

前端组件库大合集-必备收藏

前端组件库 搭建web app常用的样式/组件等收集列表(移动优先) 0. 前端自动化(Workflow) 前端构建工具 Yeoman – a set of tools for automating development workflow gulp – The streaming build system grunt – the JavaScript Task Runner F.I.S – 前端集成解决方案 前端模块管理器 Bower – A package manager for the w

前端常用框架知识点收集

前端组件库 搭建web app常用的样式/组件等收集列表(移动优先) 0. 前端自动化(Workflow) 前端构建工具 Yeoman – a set of tools for automating development workflow gulp – The streaming build system grunt – the JavaScript Task Runner F.I.S – 前端集成解决方案 前端模块管理器 Bower – A package manager for the w

前端组件收集整理列表

https://www.luoxiao123.cn/1196.html 这里是收集的web前端开发常用前端开发组件库,搭建web app常用的样式/组件等收集列表(移动优先).主要包含前端框架,构建工具,自动化模块.jQuery插件.前端样式等,囊括最实用的前端开发组件库! 0. 前端自动化 前端构建工具 gulp – The streaming build system grunt – the JavaScript Task Runner webpack(模块管理兼打包):http://web

W3C vs. WHATWG HTML5 Specs – The Differences Documented

A few weeks ago, HTML5 became an official W3C Recommendation. I took advantage of this event to discuss 5 interesting but now obsolete featureson SitePoint. The problem is that the W3C specifications are only one side of the same coin. Starting from