我有以下使用Java脚本的测试html,其中主要感兴趣的是通知:
<head>
<title>Test Notifications</title>
</head>
<body>
<div >
<button style="margin: 0;position: absolute;top: 50%;left: 50%;-ms-transform: translate(-50%, -50%);transform: translate(-50%, -50%);"
onclick="askPermissionsAndSendNotification();" >Send Notification</button>
</div>
<script type="text/javascript" >
function askPermissionsAndSendNotification() {
if (!window.Notification) {
return false;
} else {
if (Notification.permission === 'default') {
Notification.requestPermission(function (p) {
if (p !== 'denied') {
sendNotification();
}
})
} else {
sendNotification();
}
}
}
function sendNotification() {
let title = "Test notification";
let message = "Test notification content";
let myNotification = new Notification(title, {
body: message
});
myNotification.addEventListener('click', function () {
window.open("https://stackoverflow.com/","_blank");
});
}
</script>
</body>
是一个简单的通知,我们通过点击按钮来发送它,并将打开一个带有URL的新选项卡.一切都很完美,直到刷新.
如果你有一个未关闭的通知,如果你刷新页面,无论你的点击事件中的代码是什么,通知都会打开一个新页面,其中包含你的网站基础URL.我假设监听程序在页面刷新时被销毁,这可能是默认行为.如果在刷新之前创建了通知,是否可以在页面刷新后调用原始的通知onClick操作?