Reputation: 51
I'm trying to create web notifications via chrome and I'm receiving the notification and it's working fine the problem is when I'm clicking on the notification it is not disappearing please help me in closing notification when clicked/opened
I currently have
let dnperm = document.getElementById("dnperm");
let dntrigger = document.getElementById("dntrigger");
dnperm.addEventListener("click", function(e) {
e.preventDefault();
if(!window.Notification) {
alert("Sorry, Notifications are not supported!");
} else {
Notification.requestPermission(function(p) {
if (p === "denied") {
alert("You Denied");
} else if (p === "granted") {
alert("You Allowed");
}
});
}
});
dntrigger.addEventListener("click", function(e) {
let notify;
e.preventDefault();
if (Notification.permission ==="default") {
alert("Allow Notifications First!");
} else {
notify = new Notification("New Message!", {
"body": "Hello",
"icon": "favicon.png",
"tag": "123456",
"image": "img/legendary.jpg",
});
notify.onclick = function() {
window.location = "https://www.google.com/";
};
}
});
<a href="#" id="dnperm">Notifications</a>
<a href="#" id="dntrigger">Trigger</a>
Upvotes: 4
Views: 3982
Reputation: 3573
You can do this with Notification.close()
.
On the .onclick
, just run notify.close()
notify.onclick = function() {
window.location = "https://www.google.com/";
notify.close();
};
Upvotes: 1