Chrome内置支持Notifications,Firefox需要安装插件(插件下载地址:http://code.google.com/p/ff-html5notifications/)。
注意,必须在服务器中运行才会有桌面通知效果!
一个Notifications使用的小例子,代码如下:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(window).load(function(){
// 检查客户端(一般是浏览器)对Notifications的支持
//Chrome内置支持,Firefox需要安装插件
if (window.webkitNotifications) {
console.log("浏览器支持Notifications!");
// alert("浏览器支持Notifications!");
}
else {
console.log("浏览器不支持Notifications!");
// alert("浏览器不支持Notifications!");
}
});
$(document).ready(function(){
function createNotification(options) {
if (options.notificationType == 'simple') {
// 创建一个文本通知:
return window.webkitNotifications.createNotification(
'images/rails.png', // 图标路径 - 可以是相对路径
'通知', // 通知的标题
'哈哈,你中彩咯!' // 通知的内容
);
}
else if (options.notificationType == 'html') {
// 或者创建一个HTML通知:
return window.webkitNotifications.createHTMLNotification(
// HTML路径 - 可以是相对路径
'xxx.html'
);
}
}
document.querySelector('#show_button').addEventListener('click', function() {
if (window.webkitNotifications.checkPermission() == 0) {
// 0 表示允许Notifications
notification = createNotification({notificationType: 'simple'});
notification.ondisplay = function() {
console.log("display");
};
notification.onclose = function() {
console.log("close");
};
notification.show();
setTimeout('notification.cancel()', 4000)
} else {
window.webkitNotifications.requestPermission();
}
}, false);
});
</script>
</head>
<body>
<button id="show_button" type="button">点我看效果</button>
</body>
</html>
附上源代码。
分享到:
相关推荐
dingding-notifications.hpi
Laravel开发-laravel-notifications-api 通知API的实现
Laravel开发-laravel-notifications-plivo Laravel的plivo短信通知驱动程序
Laravel开发-pusher-mobile-notifications 推送器本机推送通知驱动程序。
Laravel开发-laravel-notifications Laravel 5.3和5.4的用户通知。
Laravel开发-telegram-notifications Laravel的电报通知
Arduino-push-notifications-arduino-esp8266.zip,如何使用Arduino IDE从ESP8266在手机上生成推送通知的示例。push-notifications-Arduino-ESP8266,Arduino是一家开源软硬件公司和制造商社区。Arduino始于21世纪初,...
angular2-notifications, Angular的轻量和易于使用的通知库 Angular2-NotificationsAngular 2的轻量和易于使用的通知库。 它同时具有常规页面通知( toasts ) 和推送通知。 向推送通知转移到一个单独的库 ng推送 ...
Laravel开发-laravel-notifications-pusher Laravel通知推送器
Ajax-php-notifications.zip,基于ajax的基本php通知系统,ajax代表异步javascript和xml。它是多种web技术的集合,包括html、css、json、xml和javascript。它用于创建动态网页,其中网页的小部分在不重新加载网页的...
bootstrap-notifications, Bootstrap 框架的通知组件 用于 3的 通知组件 要开始,请查看 http://skywalkapps.github.io/bootstrap-notifications 。快速启动通知组件是为 Bootstrap 3构建的。 你有以下安装选项:...
Laravel开发-pusher-push-notifications 推送器本机推送通知驱动程序。
Laravel开发-smspilot-notifications-laravel 短信试点服务通知通道
Laravel开发-laravel-push-notifications 苹果和谷歌推送通知
ZVEI Guideline-for-Customer-Notifications-PCN_V5.0-2021.pdf
Laravel开发-login-notifications Laravel的登录通知