html5桌面通知(Web Notifications)实例解析

所属分类: 网页制作 / html5 阅读数: 623
收藏 0 赞 0 分享

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

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

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


复制代码
代码如下:
<script>
var Notification = window.Notification || window.mozNotification || window.webkitNotification;

Notification.requestPermission(function (permission) {
// console.log(permission);
});

function show() {
var instance = new Notification(
"test title", {
body: " test message"
}
);

instance.onclick = function () {
// Something to do
};
instance.onerror = function () {
// Something to do
};
instance.onshow = function () {
// Something to do
};
instance.onclose = function () {
// Something to do
};

return false;
}
</script>

 
其中: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;
}

}

更多精彩内容其他人还在看

X/HTML5 和 XHTML2

在了解了XHTML 2的进展之后,我们再来看看X/HTML 5 的进展。 X/HTML 5酷在什么地方 章节元素的构想 X/HTML 5引入新的元素用于把Web页面分成若干章节。这些组成部分有助于搜索引擎和辅助工具更好地理解页面内容。使用这些新元素可以使标签更
收藏 0 赞 0 分享

HTML5 与 XHTML2

多数人使用 HTML 4 和 XHTML 1 编写网页。相对较少的 HTML 狂热者了解语义 HTML 的概念、验证 HTML 结构和改进文档的可访问性。高质量的 HTML 文档是反复权衡、设计优选和讨论酝酿的结果。尽管受到诸多批评,还没有任何语言的普及性能与 HTML 比肩。多
收藏 0 赞 0 分享

HTML5 语义化结构化规范化

HTML结构更加清晰、规范,学习HTML5优化结构的思路。 HTML5添加了一些新元素,用来标识常用的结构,是html更具语义化,可是我们无法直接使用,即使可能用到还要等他个十年八年的。 那就像微格式一样,使用class代替,或者随意点,使用id和class名来代替,让自己的结构
收藏 0 赞 0 分享

HTML5的结构和语义(5):交互

http://www.webjx.com/html_xhtml/20080306/html_xhtml_4688.html   HTML 5 也被称为 Web Applications 1.0。为了实现这个目标,增加了几个为 Web 页面提供交互体验的新元素:   
收藏 0 赞 0 分享

HTML5 WebGL 实现民航客机飞行监控系统

这篇文章主要介绍了HTML5 WebGL 的民航客机飞行监控系统,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

基于Canvas+Vue的弹幕组件的实现

这篇文章主要介绍了基于Canvas+Vue的弹幕组件的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

html通过canvas转成base64的方法

这篇文章主要介绍了html通过canvas转成base64的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

解决H5的a标签的download属性下载service上的文件出现跨域问题

这篇文章主要介绍了解决H5的a标签的download属性下载service上的文件出现跨域问题的相关资料,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Html5新增标签与样式及让元素水平垂直居中

这篇文章主要介绍了Html5新增标签与样式及让元素水平垂直居中,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

canvas实现有递增动画的环形进度条的实现方法

这篇文章主要介绍了canvas实现有递增动画的环形进度条的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享
查看更多