js html5推送 实例

时间:2022-03-23 06:12:33
<!DOCTYPE html>
  <html>
  <head>
  <title>Simple Webkit notification example</title>
  </head>
   
  <h2>设置权限</h2>
  <button id="request">允许</button>
  <h2>显示消息</h2>
  <button id="show">显示消息</button>
   
  <script type="text/javascript">
   
   
  document.getElementById('request').addEventListener('click', function() {
  window.webkitNotifications.requestPermission();
  }, false);
   
   
  document.getElementById('show').addEventListener('click', function() {
  showNotification();
  }, false);
   
  function showNotification() {
  // only show if we've got the correct permissions
  if (window.webkitNotifications.checkPermission() === 0) {
  // note the show()
  window.webkitNotifications.createNotification('http://m.poco.cn/mobile/images/app_icon-114x114.png', '欢迎进入POCO世界', 'POCO世界!').show();
  }
  }
  </script>
  </html>