html5-Notification未来的属性

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <button>Notify me!</button>
    <script>
    window.addEventListener('load', function() {
        // At first, let's check if we have permission for notification
        // If not, let's ask for it
        if (window.Notification && Notification.permission !== "granted") {
            Notification.requestPermission(function(status) {
                if (Notification.permission !== status) {
                    Notification.permission = status;
                }
            });
        }
        var button = document.getElementsByTagName('button')[0];
        button.addEventListener('click', function() {
            // If the user agreed to get notified
            // Let's try to send ten notifications
            if (window.Notification && Notification.permission === "granted") {
                for (var i = 0; i < 10; i++) {
                    // Thanks to the tag, we should only see the "Hi! 9" notification
                    var n = new Notification("Hi! " + i, {
                        tag: 'soManyNotification'
                    });
                }
            }
            // If the user hasn't told if he wants to be notified or not
            // Note: because of Chrome, we are not sure the permission property
            // is set, therefore it's unsafe to check for the "default" value.
            else if (window.Notification && Notification.permission !== "denied") {
                Notification.requestPermission(function(status) {
                    if (Notification.permission !== status) {
                        Notification.permission = status;
                    }
                    // If the user said okay
                    if (status === "granted") {
                        for (var i = 0; i < 10; i++) {
                            // Thanks to the tag, we should only see the "Hi! 9" notification
                            var n = new Notification("Hi! " + i, {
                                tag: 'soManyNotification'
                            });
                        }
                    }
                    // Otherwise, we can fallback to a regular modal alert
                    else {
                        alert("Hi!");
                    }
                });
            }
            // If the user refuses to get notified
            else {
                // We can fallback to a regular modal alert
                alert("Hi!");
            }
        });
    });
    </script>
</body>
</html>


知识共享许可协议
《html5-Notification未来的属性》常伟华 创作。
采用 知识共享 署名-相同方式共享 3.0 中国大陆 许可协议进行许可。
相邻依据:发表时间
  • 多说评论
  • 签名
  • 新浪微博
  • 默认评论
  • Tab Header 5

0 条评论 / 点击此处发表评论

Tab Content 5

开发技术


开发平台和工具

sitemap     175.70ms