blob: 3ace633ea680d5d2b3035ee0b76ac5367fb86591 (
plain)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
---
title: Notification.permission
slug: Web/API/notification/permission
translation_of: Web/API/Notification/permission
---
<p>{{APIRef("Web Notifications")}}</p>
<p><code>Notification 的只读属性 permission 用来表明用户是否允许当前域显示Web Notification. </code></p>
<p>{{AvailableInWorkers}}</p>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">var <em>permission</em> = Notification.permission;</pre>
<h3 id="Return_Value" name="Return_Value">Value</h3>
<p>permission 的类型为 {{domxref("DOMString")}} . 该属性的可能值为:</p>
<ul>
<li><code>granted</code>: 用户已经明确的授予了显示通知的权限。.</li>
<li><code>denied</code>: 用户已经明确的拒绝了显示通知的权限。</li>
<li><code>default</code>: 用户还未被询问是否授权; 这种情况下权限将视为 <code>denied</code>.</li>
</ul>
<h2 id="Examples">Examples</h2>
<p>下面的代码片段详细的说明了,当你首次检查浏览器是否支持Notification,然后检查当前域是否被授予了发送Notification的权限,并且在发送一个通知前进行请求的用法.</p>
<pre class="brush: js">function notifyMe() {
// Let's check if the browser supports notifications
if (!("Notification" in window)) {
console.log("This browser does not support desktop notification");
}
// Let's check whether notification permissions have alredy been granted
else if (Notification.permission === "granted") {
// If it's okay let's create a notification
var notification = new Notification("Hi there!");
}
// Otherwise, we need to ask the user for permission
else if (Notification.permission !== 'denied' || Notification.permission === "default") {
Notification.requestPermission(function (permission) {
// If the user accepts, let's create a notification
if (permission === "granted") {
var notification = new Notification("Hi there!");
}
});
}
// At last, if the user has denied notifications, and you
// want to be respectful there is no need to bother them any more.
}</pre>
<h2 id="Specifications">Specifications</h2>
<table>
<tbody>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
<tr>
<td>{{SpecName("Web Notifications","#dom-notification-permission","permission")}}</td>
<td>{{Spec2('Web Notifications')}}</td>
<td>Living standard</td>
</tr>
</tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
{{Compat("api.Notification.permission")}}
<h2 id="See_also">See also</h2>
<ul>
<li><a href="/en-US/docs/Web/API/Notifications_API/Using_the_Notifications_API">Using the Notifications API</a></li>
<li>{{domxref("Permissions_API","Permissions API")}}</li>
</ul>
|