asp.net中的桌面通知

时间:2015-06-01 09:19:14

标签: javascript asp.net notifications push-notification

我在asp.net中使用以下javascript进行桌面通知。在按钮上单击此事件可以正常使用权限,但从后面的代码实现时会出错。

并在我的网页上添加了此代码...                

 <script type="text/javascript">
        function notifyMe() {
            if (!("Notification" in window)) { alert("This browser does not support desktop notification"); }
            else if (Notification.permission === "granted") {
                var options = {
                    body: "This is the body of the notification",
                    icon: "stupidcodes.com.png",
                    dir: "ltr"
                };                   

                var notification = new Notification("Hi there", options);

                notification.onclick = function () {
                    window.open("http://www.stupidcodes.com/");
                };
            }
            else if (Notification.permission !== 'denied') {
                Notification.requestPermission(function (permission) {
                    if (!('permission' in Notification)) {
                        Notification.permission = permission;
                    }
                    if (permission === 'granted') {
                        var options = {
                            body: 'This is the body of the notification',
                            icon: 'stupidcodes.com.png',
                            dir: 'ltr'
                        };
                        var notification = new Notification('Hi there', options);
                        notification.onclick = function () {
                            window.open('http://www.stupidcodes.com/');
                        };
                    }
                });
            }
        }

是否有任何方法可以在asp.net

中添加这样的通知

您可以查看此代码是否有效here

1 个答案:

答案 0 :(得分:0)

请查看my blog post。我创建了一个示例,它将在加载文档时触发通知。您可以根据自己的要求使用代码。