user3274226
user3274226

Reputation: 63

Web Notification Display Duration

I am sending a notification web. I want to display up to ten minutes if the user does not click on the notification.

I used setTimeout, but it is displayed for about 15 seconds and then hidden. please guide me.

This is my code:

function notify(title, message, link) {
    var option = {
        body: message,
        dir: 'rtl',
        title: title,
        icon: '/Images/notification.png',
    }

    var notify = new Notification(title, option);

    notify.onclick = function () {
        window.open(link, '_blank');
        notify.close();
    };

    notification.onshow = function () {
        setTimeout(notification.close, 600000);
    }
}

Upvotes: 3

Views: 791

Answers (2)

Saurabh Yadav
Saurabh Yadav

Reputation: 3386

i have update your code. May this helps you !

var options = {
            body: "My notification message",
            dir : "ltr",
            requireInteraction: true
};

var notify = new Notification('Hello User', options);
notify.onclick = function () {
    notify.close();
};

notify.onshow = function () {
    setTimeout(()=>{
        notify.close();
    }, 15000);
}

Upvotes: 1

Patrick Wozniak
Patrick Wozniak

Reputation: 1572

Just add the property requireInteraction.

var option = {
    body: message,
    dir: 'rtl',
    title: title,
    icon: '/Images/notification.png',
    requireInteraction: true,
}

The requireInteraction read-only property of the Notification interface returns a Boolean indicating that a notification should remain active until the user clicks or dismisses it, rather than closing automatically.

See here: https://developer.mozilla.org/en-US/docs/Web/API/notification/requireInteraction

Upvotes: 1

Related Questions