- <!DOCTYPE html>
- <html>
- <head>
- <title>桌面通知</title>
- </head>
- <body>
- <input type="button" value="显示通知" onclick="checkPermissionAndShow()">
- <script>
- window.Notifications = window.Notifications || window.webkitNotifications;
- function checkPermissionAndShow() {
- var permissionLevel = window.Notifications.checkPermission();
- if (permissionLevel == 0) {
- console.log('已授权...');
- showNotification();
- } else if (permissionLevel == 1) {
- console.log('待授权...');
- window.Notifications.requestPermission(checkPermissionAndShow);
- } else {
- console.log('已禁止...');
- }
- }
- function showNotification() {
- var newNotification = Notifications.createNotification(
- 'http://www.codesnippet.cn/images/logo.png',
- '桌面通知',
- '桌面通知 示例');
- newNotification.show();
- }
- </script>
- </body>
- </html>
- //该片段来自于http://www.codesnippet.cn/detail/091020136303.html
来源: http://www.codesnippet.cn/detail/091020136303.html