Web通知显示持续时间

时间:2019-01-30 12:05:16

标签: javascript notifications

我正在发送通知网站。如果用户未单击通知,我最多希望显示十分钟。

我使用了setTimeout,但是显示了大约15秒钟,然后隐藏了。 请引导我。

这是我的代码:

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);
    }
}

2 个答案:

答案 0 :(得分:0)

只需添加属性requireInteraction

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

Notification的requireInteraction只读属性   接口返回一个布尔值,指示通知应   保持活动状态,直到用户单击或关闭它为止,而不是   自动关闭。

参见此处:https://developer.mozilla.org/en-US/docs/Web/API/notification/requireInteraction

答案 1 :(得分:0)

我已经更新了您的代码。可能对您有帮助!

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);
}
相关问题