2017-08-24 50 views
0

我經歷了這麼多網頁,但無法找到正確的答案,你能幫我嗎。我想知道如何創建網絡推送通知(不適用於android應用程序,但僅適用於網站)?創建網絡推送通知(不適用於android應用程序,但僅適用於網站)?

+1

您應該**嘗試自己編寫代碼**。在[**做更多的研究**之後](https://meta.stackoverflow.com/q/261592/1011527)如果你有問題**發佈你已經嘗試過**的**清楚的解釋不工作**並提供[最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)。閱讀[如何問](http://stackoverflow.com/help/how-to-ask)一個很好的問題。請務必[參觀](http://stackoverflow.com/tour)並閱讀[this](https://meta.stackoverflow.com/q/347937/1011527)。 –

回答

0

您可以簡單地添加這行代碼:

var myNotification = new Notification(title, options); 

我給你一個更好的例子,你要問用戶是否可以添加通知:

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

    // Let's check whether notification permissions have alredy 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.permission === "default") { 
    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. 
} 

您可諮詢MDN's documentationMDN's specification

+0

謝謝S.P.H.I.N.X ......如何獲得客戶的身份證,以便我可以在下次向他發送通知 – sarfaraz

+0

我不知道客戶端的ID是什麼,但是您可以使用localStorage在用戶的計算機上獲取ID。 –

相關問題