2017-08-29 118 views
1

我是新增瀏覽器通知。如何使用瀏覽器通知?我無法弄清楚從哪裏開始。任何人都可以提供關於如何開始這個建議。如何使用瀏覽器通知

回答

0

對於瀏覽器通知工作您的網站或應用程序應通過https否則瀏覽器不會允許它

mesg = { 
    title: "notification title", 
    body: "notification body", 
    icon: "location to an .ico image", 
    timer: true //for auto closing 
} 

// Let's check if the browser supports notifications 
    if (!('Notification' in window)) { 

     console.log('Browser does not support this feature.'); 

    }else if (Notification.permission === 'granted') { 

     Notification.requireInteraction = false; 

     if (mesg.title !== 'undefined') { 

     const notification = new Notification(mesg.title, { 
      body: mesg.body, 
      icon: mesg.icon 
     }); 

     if (mesg.timer) { 

      setTimeout(function() { 
      notification.close(); 
      }, 5000); 
     } 

     return notification; 

     }// close if undefined 

    } else if (Notification.permission !== 'denied') { 

     alert('Please click Allow for enabling browser notifications'); 

     Notification.requestPermission(function (permission) { 
     // If the user accepts, let's create a notification 
     if (permission === 'granted') { 

      if (mesg.title !== 'undefined') { 

       const notification = new Notification(mesg.title, { 
       body: mesg.body, 
       icon: mesg.icon 
       }); 

       if (mesg.timer) { 
       setTimeout(function() { 
        notification.close(); 
       }, 5000); 
       } 

       return notification; 

      }// close if undefined 

      } else { 

      alert('Permission Denied :['); 

      } 
     }); 
     } 

我用這個我的應用程序,你可以重構它來除去重複的代碼。

0

檢查Notification API爲引用

代碼請求權限

document.addEventListener('DOMContentLoaded', function() { 
     if (!Notification) { 
      alert('Desktop notifications not available in your browser. Try Chromium.'); 
      return; 
     } 

     if (Notification.permission !== "granted") 
      Notification.requestPermission(); 
    }); 

和用於顯示通知

 if (Notification.permission !== "granted") 
        Notification.requestPermission(); 
       else { 
        var notification = new Notification('Notification title', { 
         icon: 'Icon Link', 
         body: "Notification Body", 
        }); 

        notification.onclick = function() { 
         window.open("Href Here"); 
        }; 

       } 
0

例如

function notifyMe() { 
    // Let's check if the browser supports notifications 
    if (!("Notification" in window)) { 
    alert("This browser does not support desktop notification"); 
    } 

    // Let's check whether notification permissions have already been granted 
    else if (Notification.permission === "granted") { 
    // If it's okay let's create a notification 
    var notification = new Notification("Hi there!"); 
    } 

    // Otherwise, we need to ask the user for permission 
    else if (Notification.permission !== "denied") { 
    Notification.requestPermission(function (permission) { 
     // If the user accepts, let's create a notification 
     if (permission === "granted") { 
     var notification = new Notification("Hi there!"); 
     } 
    }); 
    } 

    // At last, if the user has denied notifications, and you 
    // want to be respectful there is no need to bother them any more. 
} 
notifyMe(); 

月re info https://developer.mozilla.org/id/docs/Web/API/notification