Navarr's Tech Side The Technical Side of my Life

12May/100

Closing Desktop Notifications on a Timer

So, one of the few things about Chrome’s desktop notifications I’ve been trying to figure out is how to close them on a timer, and it finally came to me.

This timer will only activate when the notification is opened, and will close the notification even if the page that originally spawned it has been closed.

Create and Show a new HTMLNotification.

<!DOCTYPE html>
<html>
	<head>
		<title>Notification Title</title>
		<style type="text/css">
			body,body* { font-family: sans-serif; font-size: 10pt; }
			h1 { font-family: sans-serif; margin: 0; padding: 0; font-size: 12pt; font-weight: bold; }
			p { margin: 0; padding: 0; }
		</style>
	</head>
	<body onload="setTimeout(self.close,5000)">
		<h1>Notification Title</h1>
		<p>Notification Text</p>
	</body>
</html>

That will close the window after five seconds and will look just like a normal notification! :D

What I learned is: Notification Windows respond to the javascript method self.close()

Filed under: General Leave a comment