快速实现显示浏览器通知

2017-04-18  本文已影响0人  lr3800
浏览器通知

JavaScript Web Notification API允许电脑和手机浏览器通过自定义内容显示通知。虽然支持过程不一致,但API现在与大多数现代浏览器兼容,浏览器通知在许多网站广泛使用。

本文带给大家的教程就是如何快速的使用Push.js开发浏览器通知。演示效果

开始

首先引入Push.js 该文件。

 <script src="http://cdn.52qdw.cn/push/push.min.js"></script>

请求许可

用户需要授权才能发送通知,浏览器会提示用户是否接受该网站的通知。

授权

当用户第一次浏览网页Push.js会自动请求许可,需要事先手动询问用户:

Push.Permission.request();

创建通知

要显示一个通知,我们调用Push.create`方法

Push.create('Hi there!', {
    body: 'This is a notification.',
    icon: 'icon.png',
    timeout: 8000,               // 通知超时关闭时间
    vibrate: [100, 100, 100],    
    onClick: function() {
        // 单击通知返回参数
        console.log(this);
    }  
});

选项

兼容性

大多数的浏览器都支持Notification API。点击演示查看是否支持你的浏览器。经测Chrome,Firefox和Safari以及Chrome for Android中可以正常显示通知。

完整代码演示

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Demo: The Easiest Way To Show Browser Notifications</title>
    <style>
        body {
            font-family: sans-serif;
            color: #333435;
            line-height: 1.5;      
            text-align: center;
            padding: 50px;
        }
        h1 {
            max-width: 400px;
            line-height: 1.4;
            margin: 0 auto 40px;
            text-align: center;
            color: #21629b;
            font-size: 29px;    
        }
        p {
            font-size: 16px;
            margin: 15px auto;
            text-align: center;
            max-width: 430px;
        }
        a, a:visited, a:hover {
            text-decoration: none;
            color: #267ac3;
        }
        #demo-btn {
            padding: 18px;
            color: #fff;
            background-color: #1E88E5;
            outline: 0;
            border: 0;
            font-size: 16px;
            text-transform: uppercase;
            cursor: pointer;
            opacity: 0.9;
            border-radius: 4px;
            margin: 45px auto;
            display: block;
            font-weight: bold;
        }
        #demo-btn:hover {
            opacity: 1;
        }
        p.footer {
            color: #889098;
            font-size: 15px;
            margin: 0 auto;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>The Easiest Way To Show Browser Notifications</h1>
        <p>This simple demo aims to show you the easiest possible way to display web notifications using <a href="https://github.com/Nickersoft/push.js" target="_blank">Push.js</a>.</p> 

        <button id="demo-btn">Show notification</button>

        <p class="footer">To read the full article go to <a href="http://tutorialzine.com/2017/01/the-easiest-way-to-show-browser-notifications">tutorialzine.com</a></p>    
        <p class="footer">The source code for this demo is available on <a href="https://github.com/tutorialzine/web-notifications-demo">GitHub</a></p>
    </div>
    

</body>
<script src="http://cdn.52qdw.cn/push/push.min.js"></script>
<script>
    Push.Permission.request();
    document.getElementById('demo-btn').onclick = function () {
        Push.create('Hi there!', {
            body: 'This is a notification.',
            icon: 'icon.png',
            timeout: 8000,                  
            vibrate: [100, 100, 100],       
            onClick: function() {
                // Callback for when the notification is clicked. 
                console.log(this);
            }  
        });
    };
</script>
</html>
上一篇 下一篇

猜你喜欢

热点阅读