No notification appears in chrome only in firefox [duplicate]

0

I can not make the notification work in chrome, only in mozilla, someone has a solution ????

        //Verifica e solicita se o usuario tem permissao para utilizar as notificações do Chrome
        document.addEventListener('DOMContentLoaded', function () {
            if (!Notification) {
                alert('Erro no sistema de notificação, navegador não suportado.');
                return;
            }

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

        function minhaNotificao() {
            if (Notification.permission !== "granted") {
                Notification.requestPermission();
            }
            else {
                var notificacao = new Notification("Titulo da notificacao", {
                    icon: 'go.jpg', //img
                    body: 'Mensagem'
                });

                notificacao.onclick = function () {
                    window.open('http://google.com/'); //site
                };
            }
        }

        minhaNotificao();
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <h1>Notificações</h1>
    <input type="button" value="Notificar!" onclick="minhaNotificao()">
   
</body>

</html>
    
asked by anonymous 19.11.2018 / 20:13

1 answer

1

There is a missing% as_% is not allowed to be used by the user, but all his code is meaningless, has an event that executes after else and another that executes deliberately when the script is loaded.

To summarize you have to use DOMContentLoaded in callback to check the permissions when not guaranteed and if it is different from Notification.requestPermission you can inform the user that he denied access, then just reset the settings in the browser to or the site for it to work.

function notificar()
{        
    var notificacao = new Notification('Titulo da notificacao', {
        icon: 'go.jpg', //img
        body: 'Mensagem'
    });

    notificacao.onclick = function () {
        window.open('http://google.com/'); //site
    };
}

function semPermissao()
{
    console.warn('O usuário não permitiu notificações');
}

function minhaNotificao()
{
    if (!('Notification' in window)) {
        console.warn("Este browser não suporta notificações de Desktop");
    } else if (Notification.permission === 'granted') {
        notificar();
    } else if (Notification.permission !== 'denied') {
        Notification.requestPermission(function (permission) {
            if (permission === 'granted') {
                notificar();
            } else {
                semPermissao();
            }
        });
    } else {
        semPermissao();
    }
}

document.addEventListener('DOMContentLoaded', minhaNotificao);

Note that in Gecko 46 (probably Firefox 45) the use of the callback is obsolete, one should use promises , for example:

function notificar()
{        
    var notificacao = new Notification("Titulo da notificacao", {
        icon: 'go.jpg', //img
        body: 'Mensagem'
    });

    notificacao.onclick = function () {
        window.open('http://google.com/'); //site
    };
}

function semPermissao()
{
    console.warn("O usuário não permitiu notificações");
}

function minhaNotificao()
{
    if (!('Notification' in window)) {
        console.warn("Este browser não suporta notificações de Desktop");
    } else if (Notification.permission === "granted") {
        notificar();
    } else if (Notification.permission !== 'denied') {
        Notification.requestPermission().then(function (permission) {
            if (permission === "granted") {
                notificar();
            } else {
                semPermissao();
            }
        });
    } else {
        semPermissao();
    }
}

document.addEventListener('DOMContentLoaded', minhaNotificao);

Enabling notifications in browsers

If the CHROME browser console displays the message:

  

The user did not allow notifications

Then navigate to this address

chrome://settings/content/notifications

Then click the button of the site that is blocked and select "Allow" (if your browser is in Portuguese select "Allow"):

  

InFirefox,navigatetograntedsolookforthis(orsomethingsimilar):

  

ThenonthesiteyouwanttoallowselectAllow:

  

    
20.11.2018 / 18:54