Unable to show Desktop Notifications using Google Chrome

谁都会走 提交于 2019-12-25 04:19:09

问题


I followed the instructions as given in Using The Notifications API. Also I faced many problems like the below, because I added the document.querySelector() inside the <head> part:

Uncaught TypeError: Cannot call method 'addEventListener' of null

Now I have the below source, where I am able to Check Notification Support, and Check Notification Permissions links.

Guide me how to bring in notifications in a simpler way. Also, I tried this:

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

Now I am stuck with this source. I need to generate HTML & Simple Notifications. Am I missing something? Please guide me.

Source:

<!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:


After creating the notification, you need to call show() on it, so instead of just:

createNotificationInstance({ notificationType: 'simple' });

you need to do:

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

The other thing is: when doing jQuery code, wrap it inside

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

When doing actions on the DOM with jQuery inside the head, the DOM isn't build yet. $(document).ready waits until the DOM is build and you can savely access and manipulate it.

Here is a working example: http://jsfiddle.net/fkMA4/

BTW: I think HTML notifications are deprecated, see here: http://www.html5rocks.com/en/tutorials/notifications/quick/?redirect_from_locale=de

Notifications with HTML content have been deprecated. Samples and text have been modified accordingly.



来源:https://stackoverflow.com/questions/12193953/unable-to-show-desktop-notifications-using-google-chrome

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!