-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotificationAPI.html
More file actions
66 lines (58 loc) · 2.01 KB
/
notificationAPI.html
File metadata and controls
66 lines (58 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<!DOCTYPE HTML>
<html lang="en">
<head>
<!-- basic.html -->
<title>notificationAPI</title>
<meta charset="UTF-8" />
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h1>The Notification API</h1>
<p>
This API allows web apps to send notifications at system level and not only at website level. <br>
This way an application can notify the user, even when it is idle or in background. <br>
An typical use-case would be a chat or mail application, where you want to user get the message immediatly. <br>
To use this API you must check for the right permissions, they can be accessed via <br>
<code> Notification.permission </code> <br>
and can be <br>
<ul>
<li><b>default</b> - the user hasn't been asked yet, so no notifications</li>
<li><b>denied</b> - no notificaitons can be displayed</li>
<li><b>granted</b> - you are good to go and can display notificaitons</li>
</ul>
</p>
<p>
Dont worry if you dont have the right permissions set, they can be requested like this:<br>
<code>
if (Notification.permission !== "denied") { <br>
Notification.requestPermission().then(function (permission) { <br>
//Do whatever you want to do <br>
});
</code> <br>
And finally the notification is created like this: <br>
<code>
new Notification("Hi there!");
</code> <br>
Try it out below!
<br>
</p>
<button onclick="notifyMe()">Notify me!</button>
<script>
function notifyMe() {
if (!("Notification" in window)) {
alert("This browser does not support desktop notification");
}
else if (Notification.permission === "granted") {
new Notification("Hello World!");
}
else if (Notification.permission !== "denied") {
Notification.requestPermission().then(function (permission) {
if (permission === "granted") {
new Notification("Hi there!");
}
});
}
}
</script>
<a href="index.html"> back </a>
</body>