Notification in javascript/HTML 5

Asked

Viewed 251 times

0

I used javascript below to display notifications. It works and is called 10 in 10 seconds. However, when two notification windows happen to appear, only one is automatically closed. Is it possible for me to close it after a while or is it always the user who has to close it manually? Thank you.

    function notify(mensagem) {
        Notification.requestPermission(function() {
                var notification = new Notification("Vai aparecer notificação!", {

                icon: 'favicon.ico', 
                icon: 'logo.png',
                body: mensagem

            });
            notification.onclick = function() {

                window.open("http://www.site.com");

            }


        });
    } 

1 answer

0

I was able to solve it. I used setTimeout. The corrected function is below, for those who need =D

function notify(mensagem) {
    Notification.requestPermission(function() {
    var notification = new Notification("Vai aparecer notificação!", {

        icon: 'favicon.ico', 
        icon: 'logo.png',
        body: mensagem


    });
    notification.onclick = function() {

        window.open("http://www.site.com");

    }
    // Fecha após 3 segundos
    setTimeout(notification.close.bind(notification), 3000);


   });
} 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.