SonickSeven
SonickSeven

Reputation: 594

How to hide a notification

I have a script that shows a notification (HTML5), but after some time I want this notification to hide.

Is this possible? How do I hide the notification?

My code to do a notification:

function notifyMe(data){
    if (!("Notification" in window)) {
        console.log("This browser does not support desktop notification");
    }else if (Notification.permission !== 'denied') {
        Notification.requestPermission(function (permission) {
            if (permission === "granted"){
                var notification = new Notification("FOTÓGENA", data);
                setTimeout(function(){
                   #hide the notification
                }, 3000)
            }
        });
    }else
        console.log('Por favor dale en permitir a las notificaciones')
}

Upvotes: 0

Views: 199

Answers (2)

taxicala
taxicala

Reputation: 21769

Use the .close method of the notification api:

function notifyMe(data){
    if (!("Notification" in window)) {
        console.log("This browser does not support desktop notification");
    }else if (Notification.permission !== 'denied') {
        Notification.requestPermission(function (permission) {
            if (permission === "granted"){
                var notification = new Notification("FOTÓGENA", data);
                setTimeout(function(){
                   notification.close()
                }, 3000)
            }
        });
    }else
        console.log('Por favor dale en permitir a las notificaciones')
}

Upvotes: 1

Scott
Scott

Reputation: 5379

notification.close() should do the trick.

Upvotes: 2

Related Questions