如何让通知保持打开状态直到用户关闭它? [英] How do I make a Notification stay open until the user closes it?
本文介绍了如何让通知保持打开状态直到用户关闭它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有设置可以让我保持通知打开直到用户单击它?
Is there a setting that will allow me to keep a Notification open until a user clicks it?
if (("Notification" in window)) {
Notification.requestPermission(function() {
var notification =
new Notification('Hello',
{
body : 'Hello',
icon: 'https://www.domain.com/images/live_chat_icon.png',
tag: 'Test' ,
});
notification.onclick = function(event) {
event.preventDefault(); // prevent the browser from focusing the Notification's tab
window.open('https://www.domain.com', '_blank');
}
window.navigator.vibrate(500);
});
}
推荐答案
根据文档,有一个布尔值 requireInteraction
:
According to the docs there is a boolean requireInteraction
:
一个布尔值,指示在具有足够大屏幕的设备上,通知应保持活动状态,直到用户单击或关闭它.https://developer.mozilla.org/en-US/docs/Web/API/通知
A Boolean indicating that on devices with sufficiently large screens, a notification should remain active until the user clicks or dismisses it. https://developer.mozilla.org/en-US/docs/Web/API/notification
new Notification('Hello', {
body : 'Hello',
requireInteraction: true
});
在 MacOS 上的 Chrome 中测试.
Tested in Chrome on MacOS.
这篇关于如何让通知保持打开状态直到用户关闭它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文