Notification popup doesn't appear in chrome

 ̄綄美尐妖づ 提交于 2020-01-15 09:22:11

问题


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 coming . 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.


回答1:


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 compatibility and things like that.




回答2:


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.net/stackexchange/img/logos/so/so-icon.png',
          body: "Hey there! You've been notified!",
        });

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

      }

    }

    notifyMe();

    Call notifyMe(); to show notification


来源:https://stackoverflow.com/questions/24472601/notification-popup-doesnt-appear-in-chrome

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!