我的网站关闭时应弹出桌面通知

时间:2018-01-15 09:26:29

标签: push-notification service-worker

我有以下情况,当我的网站打开时启用桌面通知。即使我的网站关闭,我也想做同样的事情。我正在使用RESTAPI从后端触发通知。那么如果网站关闭,我们如何显示通知。

   <a href="#" id="dnperm">Request Permission</a>
                <a href="#" id="dntrigger">Trigger</a>
            <script>
            var dnperm = document.getElementById('dnperm');
            var dntrigger = document.getElementById('dntrigger');

                dnperm.addEventListener('click',function(e){
                e.preventDefault();
            if(!window.Notification){
                alert('Sorry, Notifications are not supported');
            }
                    else{
                        Notification.requestPermission(function(p){
    console.log(p);                 });
                    }

                });
                //simulate an event             


     dntrigger.addEventListener('click',function(e){
            var notify;
            e.preventDefault();
            console.log(Notification.permission);

            if(Notification.permission === 'default'){
                alert('please allow notification'); }
            else{
                notify = new Notification('new message for you Nicoleta' ,{
                body:'how are you ',
                icon:'assets/images/logo.jpg'
            });
                notify.onlclick = function(){
        window.location = 

    '?message='+this.tag;       }
            }
        }); 


            </script>

0 个答案:

没有答案