From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../web/api/notification/permission/index.html | 179 +++++++++++++++++++++ 1 file changed, 179 insertions(+) create mode 100644 files/zh-cn/web/api/notification/permission/index.html (limited to 'files/zh-cn/web/api/notification/permission/index.html') diff --git a/files/zh-cn/web/api/notification/permission/index.html b/files/zh-cn/web/api/notification/permission/index.html new file mode 100644 index 0000000000..7cd16cf9d1 --- /dev/null +++ b/files/zh-cn/web/api/notification/permission/index.html @@ -0,0 +1,179 @@ +--- +title: Notification.permission +slug: Web/API/notification/permission +translation_of: Web/API/Notification/permission +--- +

{{APIRef("Web Notifications")}}

+ +

Notification 的只读属性 permission 用来表明用户是否允许当前域显示Web Notification. 

+ +

{{AvailableInWorkers}}

+ +

Syntax

+ +
var permission = Notification.permission;
+ +

Value

+ +

permission 的类型为 {{domxref("DOMString")}} . 该属性的可能值为:

+ + + +

Examples

+ +

下面的代码片段详细的说明了,当你首次检查浏览器是否支持Notification,然后检查当前域是否被授予了发送Notification的权限,并且在发送一个通知前进行请求的用法.

+ +
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.
+}
+ +

Specifications

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName("Web Notifications","#dom-notification-permission","permission")}}{{Spec2('Web Notifications')}}Living standard
+ +

Browser compatibility

+ +

{{ CompatibilityTable() }}

+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support5 {{ property_prefix("webkit") }} (see notes)
+ 22
{{CompatVersionUnknown}}4.0 {{ property_prefix("moz") }} (see notes)
+ 22
{{ CompatNo() }}256 (see notes)
Available in workers{{CompatUnknown}}{{CompatUnknown}}{{CompatGeckoDesktop("41.0")}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidAndroid WebviewEdgeFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari MobileChrome for Android
Basic support{{ CompatUnknown() }} +

{{CompatVersionUnknown}}

+
{{CompatVersionUnknown}}4.0 {{ property_prefix("moz") }} (see notes)
+ 22
1.0.1 {{ property_prefix("moz") }} (see notes)
+ 1.2
{{ CompatNo() }}{{ CompatUnknown() }}{{ CompatNo() }} +

{{CompatVersionUnknown}}

+
Available in workers{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatGeckoMobile(41.0)}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
+
+ +

Firefox OS notes

+ +

{{Page("/en-US/docs/Web/API/Notifications_API", "Firefox OS notes")}}

+ +

Chrome notes

+ +

{{Page("/en-US/docs/Web/API/Notifications_API", "Chrome notes")}}

+ +

Safari notes

+ +

{{Page("/en-US/docs/Web/API/Notifications_API", "Safari notes")}}

+ +

See also

+ + -- cgit v1.2.3-54-g00ecf