您现在的位置是:网站首页> 编程资料编程资料
html5桌面通知(Web Notifications)实例解析Html5中的桌面通知Notification的实现HTML5实现桌面通知 提示功能HTML5实现Notification API桌面通知功能 突袭HTML5之Javascript API扩展5—其他扩展(应用缓存/服务端消息/桌面HTML5中的Web Notification桌面通知功能的实现方法
2021-09-01
1345人已围观
简介 这篇文章主要为大家介绍了html5桌面通知(Web Notifications)实例,对于进行html5网站建设非常有用!需要的朋友可以参考下
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;
}
});
}
});
// 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.
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;
}
}
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实现WebSocket协议原理浅析HTML5+WebSocket实现多文件同时上传的实例HTML5-WebSocket实现聊天室示例浅析HTML5的WebSocket与服务器推送事件html5的websockets全双工通信详解学习示例利用html5的websocket实现websocket聊天室html5-websocket基于远程方法调用的数据交互实现基于HTML5的WebSocket的实例代码
- HTML5新增的表单元素和属性实例解析详解HTML5表单新增属性HTML Form表单元素全面了解html5表单及新增的改良元素详解HTML5 的新的表单元素(datalist/keygen/output)使用介绍HTML5之HTML元素扩展(下)—增强的Form表单元素值得关注HTML标记语言——表单HTML表单标记教程(2):HTML表单标记教程(4):带有图片预览功能的上传表单的完整HTMLXHTML入门学习教程:表单标签
- HTML5+CSS3实现拖放(Drag and Drop)示例5分钟弄清楚html5的drag and drop(小结)HTML5 拖放(Drag 和 Drop)详解与实例代码详解HTML5中的拖放事件(Drag 和 drop)突袭HTML5之Javascript API扩展4—拖拽(Drag/Drop)概述HTML5 drag和drop具体使用详解
- html5的自定义data-*属性与jquery的data()方法的使用详解HTML5 data-* 自定义属性HTML5自定义属性前缀data-及dataset的使用方法(html5 新特性)全面解析HTML5中的标准属性与自定义属性HTML5的自定义属性data-*详细介绍和JS操作实例HTML5自定义data-* data(obj)属性和jquery的data()方法的使用HTML5自定义属性的问题分析
- Html5新特性用canvas标签画多条直线附效果截图HTML5 canvas标签实现刮刮卡效果HTML5 Canvas标签使用收录详解HTML5 Canvas标签及基本使用
- HTML5在canvas中绘制复杂形状附效果截图html5使用html2canvas实现浏览器截图的示例HTML5+CSS3模仿优酷视频截图功能示例canvas与html5实现视频截图功能示例Html5新特性用canvas标签画多条直线附效果截图html+css+jquery模仿搜索风云榜选项卡效果有截图HTML5 Canvas实现平移/放缩/旋转deom示例(附截图)使用HTML截图并保存为本地图片的实现代码
- HTML5 在canvas中绘制矩形附效果图html5实现点击弹出图片功能html5 录制mp3音频支持采样率和比特率设置html5表单的required属性使用html5调用摄像头实例代码HTML5页面音频自动播放的实现方式Html5大屏数据可视化开发的实现html实现弹窗的实例HTML5来实现本地文件读取和写入的实现方法HTML 罗盘式时钟的实现HTML5简单实现添加背景音乐的几种方法
- HTML5如何为形状图上颜色怎么绘制具有颜色和透明度的矩形html5实现点击弹出图片功能html5 录制mp3音频支持采样率和比特率设置html5表单的required属性使用html5调用摄像头实例代码HTML5页面音频自动播放的实现方式Html5大屏数据可视化开发的实现html实现弹窗的实例HTML5来实现本地文件读取和写入的实现方法HTML 罗盘式时钟的实现HTML5简单实现添加背景音乐的几种方法
- HTML5 在canvas中绘制文本附效果图html5实现点击弹出图片功能html5 录制mp3音频支持采样率和比特率设置html5表单的required属性使用html5调用摄像头实例代码HTML5页面音频自动播放的实现方式Html5大屏数据可视化开发的实现html实现弹窗的实例HTML5来实现本地文件读取和写入的实现方法HTML 罗盘式时钟的实现HTML5简单实现添加背景音乐的几种方法
- HTML5使用drawImage()方法绘制图像html5实现点击弹出图片功能html5 录制mp3音频支持采样率和比特率设置html5表单的required属性使用html5调用摄像头实例代码HTML5页面音频自动播放的实现方式Html5大屏数据可视化开发的实现html实现弹窗的实例HTML5来实现本地文件读取和写入的实现方法HTML 罗盘式时钟的实现HTML5简单实现添加背景音乐的几种方法
