最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Notification popup doesn't appear in chrome - Stack Overflow

programmeradmin2浏览0评论

I am trying to do a gmail similar type desktop notification . I am facing the difficulty to change the notification permission in chrome. The permission always shows denied in console if i use window.Notification.permission. If i manually change the permission in google chrome settings -> privacy -> content settings -> Notifications as "Allow all sites to show desktop notifications" from "Do not allow any site to show desktop notifications" . Now i am able to get desktop notification normally. But I need an alert to asking a permission if the browser have setting as "Do not allow any site to show desktop notifications" , then i need to choose allow from the popup in order to change the setting as "Allow all sites to show desktop notifications". The problem is permission is not changed if i do like this even the permission alert is not ing . The permission checking script follows

if(Notification.permission == 'denied'){
                 Notification.requestPermission(function (status){
                        console.log("Reaching here");
                        Notification.permission = status;
                     });
            }

The popup for requesting permission to allow or disallow notifications is not occurred. Thanks in advance for suggesting me a solution.

I am trying to do a gmail similar type desktop notification . I am facing the difficulty to change the notification permission in chrome. The permission always shows denied in console if i use window.Notification.permission. If i manually change the permission in google chrome settings -> privacy -> content settings -> Notifications as "Allow all sites to show desktop notifications" from "Do not allow any site to show desktop notifications" . Now i am able to get desktop notification normally. But I need an alert to asking a permission if the browser have setting as "Do not allow any site to show desktop notifications" , then i need to choose allow from the popup in order to change the setting as "Allow all sites to show desktop notifications". The problem is permission is not changed if i do like this even the permission alert is not ing . The permission checking script follows

if(Notification.permission == 'denied'){
                 Notification.requestPermission(function (status){
                        console.log("Reaching here");
                        Notification.permission = status;
                     });
            }

The popup for requesting permission to allow or disallow notifications is not occurred. Thanks in advance for suggesting me a solution.

Share Improve this question asked Jun 29, 2014 at 2:16 RajanRajan 4262 gold badges7 silver badges28 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

There seem to be a problem with your script.

It's part of the standard that when the permission is set to denied you can NEVER show the popup which asks "Do you want to allow... to send desktop notifications ?". This popup is used only in the case the permission is set to default, which in fact means that the user doesn't care and you should ask him if he wants them or not.

This is the conditionnal I use :

Notification['permission'] !== 'granted' && Notification['permission'] !== 'denied'

Because the default value isn't supported by all browsers. Plus the permission attribute was not implemented prior to chrome 32, that's why you should access it using the square brackets.

In fact you could also remove the denied part in my conditionnal, because it won't do anything if the permission is denied. You can refer to this MDN documentation to get more information on patibility and things like that.

Issue of not opening dialog box is http. Chrome desktop notification work only on https protocol.

I have faced this issue and I have spend many time to solved that. Finally I got solution using https.

Here I shared code so you can run on https and it will working fine.

enter code here
       // request permission on page load
    document.addEventListener('DOMContentLoaded', function () {
      if (!Notification) {
        alert('Desktop notifications not available in your browser. Try Chromium.'); 
        return;
      }

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

    function notifyMe() {
      if (Notification.permission !== "granted")
        Notification.requestPermission();
      else {
        var notification = new Notification('Notification title', {
          icon: 'http://cdn.sstatic/stackexchange/img/logos/so/so-icon.png',
          body: "Hey there! You've been notified!",
        });

        notification.onclick = function () {
          window.open("http://stackoverflow./a/13328397/1269037");      
        };

      }

    }

    notifyMe();

    Call notifyMe(); to show notification
发布评论

评论列表(0)

  1. 暂无评论