无法使用Google Chrome显示桌面通知

时间:2012-08-30 09:37:06

标签: html5 google-chrome webkit notifications html5-notifications

我按照Using The Notifications API中的说明进行操作。我也面临很多问题,如下所示,因为我在document.querySelector()部分添加了<head>

Uncaught TypeError: Cannot call method 'addEventListener' of null

现在我有以下来源,我可以检查通知支持检查通知权限链接。

指导我如何以更简单的方式引入通知。另外,我试过这个:

$("#html").click(function() {
    if (window.webkitNotifications.checkPermission() == 0) {
        createNotificationInstance({ notificationType: 'html' });
    } else {
        window.webkitNotifications.requestPermission();
    }
});

现在我被这个来源困住了。我需要生成HTML&amp;简单通知。我错过了什么吗?请指导我。

来源:

<!DOCTYPE HTML>
<html lang="en-US">
    <head>
        <meta charset="UTF-8">
        <title>Desktop Notifications</title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
        <script type="text/javascript">
            function checkNotifications() {
                if (window.webkitNotifications)
                    alert("Notifications are supported!");
                else
                    alert("Notifications are not supported for this Browser/OS version yet.");
            }
            function createNotificationInstance(options) {
                if (window.webkitNotifications.checkPermission() == 0) { // 0 is PERMISSION_ALLOWED
                    if (options.notificationType == 'simple') {
                        return window.webkitNotifications.createNotification('icon.png', 'Notification Title', 'Notification content...');
                    } else if (options.notificationType == 'html') {
                        return window.webkitNotifications.createHTMLNotification('http://localhost/');
                    }
                } else {
                    window.webkitNotifications.requestPermission();
                }
            }
        </script>
        <style type="text/css">
            * {font-family: Verdana, sans-serif;}
            body {font-size: 10pt; margin: 0; padding: 0;}
            p {margin: 5px;}
            a {color: #09f; text-decoration: none;}
            a:hover {color: #f00;}
        </style>
    </head>
    <body>
        <p><strong>Desktop Notifications</strong></p>
        <p>Lets see how the notifications work in this browser.</p>
        <p>
            <a href="#" onclick="checkNotifications(); return false;">Check Notification Support</a>.
            Next <a href="#" onclick="alert('Notifications are ' + ((window.webkitNotifications.checkPermission() == 0) ? '' : 'not ') + 'allowed!'); return false;">Check Notification Permissions</a>
            and if permissions are not there,
            <a href="#" onclick="window.webkitNotifications.requestPermission(); return false;">Request Permissions</a>.
            Create a
            <a href="#" id="text">Simple Notification</a>
            or
            <a href="#" id="html">HTML Notification</a>.
        </p>
    </body>
    <script type="text/javascript">
        document.querySelector("#html").addEventListener('click', function() {
            if (window.webkitNotifications.checkPermission() == 0) {
                createNotificationInstance({ notificationType: 'html' });
            } else {
                window.webkitNotifications.requestPermission();
            }
        }, false);
        document.querySelector("#text").addEventListener('click', function() {
            if (window.webkitNotifications.checkPermission() == 0) {
                createNotificationInstance({ notificationType: 'simple' });
            } else {
                window.webkitNotifications.requestPermission();
            }
        }, false);
    </script>
</html>

1 个答案:

答案 0 :(得分:4)

创建通知后,您需要在其上调用show(),而不仅仅是:

createNotificationInstance({ notificationType: 'simple' });

你需要这样做:

var n = createNotificationInstance({ notificationType: 'simple' });
n.show();

另一件事是:在执行jQuery代码时,将其包装在

$(document).ready(function() {
  // ...
  // your jQuery code
  // ...
});

当在头部内部使用jQuery对DOM执行操作时,DOM尚未构建。 $(document).ready等待DOM构建,您可以保存访问和操作它。

以下是一个工作示例:http://jsfiddle.net/fkMA4/

顺便说一句:我认为不推荐使用HTML通知,请参阅此处:http://www.html5rocks.com/en/tutorials/notifications/quick/?redirect_from_locale=de

  

已弃用包含HTML内容的通知。样品和文字   已相应修改。