`

chrome浏览器的桌面通知规范desktop notifications

    博客分类:
  • web
 
阅读更多

 

原文链接:http://www.javaarch.net/jiagoushi/663.htm

 

W3C specification:http://www.w3.org/TR/notifications/ 这是w3c的最新的浏览器桌面通知的规范,现在只有chrome支持w3c的较低版本的桌面通知规范,http://www.chromium.org/developers/design-documents/desktop-notifications/api-specification,

 

创建桌面通知会有两步骤:

1.需要获得用户的允许网站允许发送桌面通知到桌面;

2. 如果用户允许,你则可以使用chrome的桌面API发送一个桌面通知给用户。

 

这是一个示例地址:http://www.smartjava.org/examples/notifications

代码如下:可以自己放在chrome试试。

 

firefox需要这个插件的支持:https://addons.mozilla.org/en-us/firefox/addon/html-notifications/

 

	<!DOCTYPE html>
	<html>
	<head>
		<title>Simple Webkit notification example</title>
	</head>
	 
	<h2>First click the 'request permission' button</h2>
	<button id="request">Request permission</button>
	<h2>After permission is granted, click the 'show notification' button</h2>
	<button id="show">Show notification</button>
	 
	<script type="text/javascript">
	 
	 
		document.getElementById('request').addEventListener('click', function() {
			window.webkitNotifications.requestPermission();
		}, false);
	 
	 
		document.getElementById('show').addEventListener('click', function() {
			showNotification();
		}, false);
	 
		function showNotification() {
			// only show if we've got the correct permissions
			if (window.webkitNotifications.checkPermission() === 0) {
				// note the show()
				window.webkitNotifications.createNotification('images/email.jpg', 'Plain Text Notification', 'Notification from the browser!').show();
			}
		}
	</script>
	</html>

 

1
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics