2017-08-24 58 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。 –

相关问题