From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../zh-cn/web/api/notification/actions/index.html | 132 +++++++++ files/zh-cn/web/api/notification/badge/index.html | 94 +++++++ files/zh-cn/web/api/notification/body/index.html | 155 +++++++++++ files/zh-cn/web/api/notification/close/index.html | 146 ++++++++++ files/zh-cn/web/api/notification/data/index.html | 165 +++++++++++ files/zh-cn/web/api/notification/dir/index.html | 167 ++++++++++++ files/zh-cn/web/api/notification/icon/index.html | 63 +++++ files/zh-cn/web/api/notification/image/index.html | 46 ++++ files/zh-cn/web/api/notification/index.html | 264 ++++++++++++++++++ .../web/api/notification/notification/index.html | 301 +++++++++++++++++++++ .../zh-cn/web/api/notification/onclick/index.html | 58 ++++ .../zh-cn/web/api/notification/onclose/index.html | 34 +++ .../zh-cn/web/api/notification/onerror/index.html | 54 ++++ files/zh-cn/web/api/notification/onshow/index.html | 34 +++ .../web/api/notification/permission/index.html | 179 ++++++++++++ .../zh-cn/web/api/notification/renotify/index.html | 68 +++++ .../api/notification/requestpermission/index.html | 174 ++++++++++++ .../api/notification/requireinteraction/index.html | 59 ++++ files/zh-cn/web/api/notification/sound/index.html | 129 +++++++++ .../using_web_notifications/index.html | 292 ++++++++++++++++++++ 20 files changed, 2614 insertions(+) create mode 100644 files/zh-cn/web/api/notification/actions/index.html create mode 100644 files/zh-cn/web/api/notification/badge/index.html create mode 100644 files/zh-cn/web/api/notification/body/index.html create mode 100644 files/zh-cn/web/api/notification/close/index.html create mode 100644 files/zh-cn/web/api/notification/data/index.html create mode 100644 files/zh-cn/web/api/notification/dir/index.html create mode 100644 files/zh-cn/web/api/notification/icon/index.html create mode 100644 files/zh-cn/web/api/notification/image/index.html create mode 100644 files/zh-cn/web/api/notification/index.html create mode 100644 files/zh-cn/web/api/notification/notification/index.html create mode 100644 files/zh-cn/web/api/notification/onclick/index.html create mode 100644 files/zh-cn/web/api/notification/onclose/index.html create mode 100644 files/zh-cn/web/api/notification/onerror/index.html create mode 100644 files/zh-cn/web/api/notification/onshow/index.html create mode 100644 files/zh-cn/web/api/notification/permission/index.html create mode 100644 files/zh-cn/web/api/notification/renotify/index.html create mode 100644 files/zh-cn/web/api/notification/requestpermission/index.html create mode 100644 files/zh-cn/web/api/notification/requireinteraction/index.html create mode 100644 files/zh-cn/web/api/notification/sound/index.html create mode 100644 files/zh-cn/web/api/notification/using_web_notifications/index.html (limited to 'files/zh-cn/web/api/notification') diff --git a/files/zh-cn/web/api/notification/actions/index.html b/files/zh-cn/web/api/notification/actions/index.html new file mode 100644 index 0000000000..0646fd416d --- /dev/null +++ b/files/zh-cn/web/api/notification/actions/index.html @@ -0,0 +1,132 @@ +--- +title: Notification.actions +slug: Web/API/notification/actions +tags: + - Notification actions + - Notifications + - Web API + - Web Notifications API +translation_of: Web/API/Notification/actions +--- +

{{APIRef("Web Notifications")}}

+ +

{{domxref("Notification")}}接口的只读属性actions返回使用{{domxref("Notification.Notification","Notification()")}}构造函数创建通知时使用actions选项设置的{{domxref("NotificationAction")}}对象列表。这是用户可以在通知上下文中选择立即执行的应用定义的操作列表。

+ +

{{AvailableInWorkers}}

+ +

语法

+ +
var actions[] = Notification.actions;
+ +

+ +

{{domxref("NotificationAction")}}对象的只读数组。用户在通知中选择每项的单一的功能。

+ +

规范

+ + + + + + + + + + + + + + +
规范状态说明
{{SpecName('Web Notifications','#dom-notification-actions','actions')}}{{Spec2('Web Notifications')}}Living standard
+ +

浏览器兼容性

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatChrome(53)}}{{CompatUnknown}}{{CompatUnknown}}{{CompatOpera(40)}}{{CompatUnknown}}
Available in workers{{CompatChrome(53)}}{{CompatUnknown}}{{CompatUnknown}}{{CompatOpera(40)}}{{CompatUnknown}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroid WebviewChrome for AndroidFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari Mobile
Basic support +

{{CompatNo}}

+
{{CompatChrome(53)}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatOperaMobile(40)}}{{CompatUnknown}}
Available in workers{{CompatNo}}{{CompatChrome(53)}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatOperaMobile(40)}}{{CompatUnknown}}
+
+ +

Firefox OS 相关

+ +

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

+ +

Chrome 相关

+ +

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

+ +

Safari 相关

+ +

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

+ +

更多

+ + diff --git a/files/zh-cn/web/api/notification/badge/index.html b/files/zh-cn/web/api/notification/badge/index.html new file mode 100644 index 0000000000..9858d6424c --- /dev/null +++ b/files/zh-cn/web/api/notification/badge/index.html @@ -0,0 +1,94 @@ +--- +title: Notification.badge +slug: Web/API/notification/badge +tags: + - Notification + - Notification badge + - Notifications API + - Web API +translation_of: Web/API/Notification/badge +--- +

{{SeeCompatTable}}{{APIRef("Notifications API")}}

+ +

当没有足够的空间来显示通知本身时,{{domxref("Notification")}}接口的 badge 属性返回用于表示通知的图像URL。

+ +

语法

+ +
var url = Notification.badge
+ +

+ +

包含一个URL的 {{domxref('USVString')}} 。

+ +

规范

+ + + + + + + + + + + + + + +
规范状态说明
{{SpecName('Web Notifications','#dom-notification-badge','badge')}}{{Spec2('Web Notifications')}}Initial definition.
+ +

浏览器兼容性

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatChrome(53.0)}}{{CompatUnknown}}{{CompatUnknown}}{{CompatOpera(39)}}{{CompatUnknown}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidAndroid WebviewFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari MobileChrome for Android
Basic support{{CompatNo}}{{CompatChrome(53.0)}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatOperaMobile(39)}}{{CompatUnknown}}{{CompatChrome(53.0)}}
+
diff --git a/files/zh-cn/web/api/notification/body/index.html b/files/zh-cn/web/api/notification/body/index.html new file mode 100644 index 0000000000..02ada837d2 --- /dev/null +++ b/files/zh-cn/web/api/notification/body/index.html @@ -0,0 +1,155 @@ +--- +title: Notification.body +slug: Web/API/notification/body +translation_of: Web/API/Notification/body +--- +

{{APIRef("Web Notifications")}}

+ +

 {{domxref("Notification")}} 的只读属性body返回了构造函数{{domxref("Notification")}}实例化时,options所使用的body值。

+ +

{{AvailableInWorkers}}

+ +

语法

+ +
var body = Notification.body;
+
+ +

+ +

{{domxref("DOMString")}}.

+ +

示例

+ +

在 Emogotchi demo (see source code)中, 我们在激活一个桌面通知时,调用了spawnNotification()函数—函数被传入了指定的参数 body、icon、title , 创建了一个必选对象传入{{domxref("Notification.Notification","Notification()")}} 构造函数创建了一个实例.

+ +
function spawnNotification(theBody,theIcon,theTitle) {
+  var options = {
+      body: theBody,
+      icon: theIcon
+  }
+  var n = new Notification(theTitle,options);
+}
+ +

规范

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

浏览器兼容性

+ +
{{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 备忘

+ +

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

+ +

Chrome 备忘

+ +

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

+ +

Safari 备忘

+ +

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

+ +

相关链接

+ + diff --git a/files/zh-cn/web/api/notification/close/index.html b/files/zh-cn/web/api/notification/close/index.html new file mode 100644 index 0000000000..294360572c --- /dev/null +++ b/files/zh-cn/web/api/notification/close/index.html @@ -0,0 +1,146 @@ +--- +title: Notification.close() +slug: Web/API/notification/close +tags: + - Notification.close() +translation_of: Web/API/Notification/close +--- +
{{APIRef("Web Notifications")}}
+ +

{{domxref("Notification")}} 接口的 close() 的方法用于关闭一个以前显示的通知。

+ +

Syntax

+ +
Notification.close();
+ +

Parameters

+ +

None.

+ +

Returns

+ +

Void.

+ +

Examples

+ +

以下是 Emogotchi 示例在线演示)中的一段代码 ,定义了一个简单的函数spawnNotification,当spawnNotification被调用时会创建一个对象并生成一个新的Notification。在函数的最后,它在{{domxref("WindowTimers.setTimeout","setTimeout()")}} 中调用了close()函数来实现在4s后关闭Notification(有些浏览器会自动关闭弹出的Notification,但有些不是,例如Chrome,Opera)。还要注意bind()的使用,来确保close()方法绑定到Notification的实例上。

+ +
function spawnNotification(theBody,theIcon,theTitle) {
+  var options = {
+      body: theBody,
+      icon: theIcon
+  }
+
+  var n = new Notification(theTitle,options);
+  setTimeout(n.close.bind(n), 4000);
+}
+
+ +

Specifications

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Web Notifications')}}{{Spec2('Web Notifications')}}Living standard
+ +

Browser compatibility

+ +

{{CompatibilityTable}}

+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support5{{property_prefix("webkit")}}[1]
+ 22
{{CompatVersionUnknown}}{{CompatGeckoDesktop("2.0")}}{{property_prefix("moz")}}[2]
+ {{CompatGeckoDesktop("22.0")}}
{{CompatNo}}256[3]
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidAndroid WebviewEdgeFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari MobileChrome for Android
Basic support{{CompatUnknown}} +

{{CompatVersionUnknown}}

+
{{CompatVersionUnknown}}{{CompatGeckoMobile(4.0)}}{{property_prefix("moz")}}[2]
+ {{CompatGeckoMobile(22.0)}}
1.0.1{{property_prefix("moz")}}[2]
+ 1.2
{{CompatNo}}{{CompatUnknown}}{{CompatNo}} +

{{CompatVersionUnknown}}

+
+
+ +

[1] Before Chrome 22, the support for notification followed an old prefixed version of the specification and used the {{domxref("window.navigator.webkitNotifications","navigator.webkitNotifications")}} object to instantiate a new notification.

+ +

Before Chrome 32, {{domxref("Notification.permission")}} was not supported.

+ +

Before Chrome 42, service worker additions were not supported.

+ +

[2] Prior to Firefox 22 (Firefox OS <1.2), the instantiation of a new notification must be done with the {{domxref("window.navigator.mozNotification", "navigator.mozNotification")}} object through its createNotification method.

+ +

Prior to Firefox 22 (Firefox OS <1.2), the Notification was displayed when calling the show method and supported only the click and close events.

+ +

Nick Desaulniers wrote a Notification shim to cover both newer and older implementations.

+ +

One particular Firefox OS issue is that you can pass a path to an icon to use in the notification, but if the app is packaged you cannot use a relative path like /my_icon.png. You also can't use window.location.origin + "/my_icon.png" because window.location.origin is null in packaged apps. The manifest origin field fixes this, but it is only available in Firefox OS 1.1+. A potential solution for supporting Firefox OS <1.1 is to pass an absolute URL to an externally hosted version of the icon. This is less than ideal as the notification is displayed immediately without the icon, then the icon is fetched, but it works on all versions of Firefox OS.

+ +

When using notifications  in a Firefox OS app, be sure to add the desktop-notification permission in your manifest file. Notifications can be used at any permission level, hosted or above: "permissions": { "desktop-notification": {} }

+ +

[3] Safari started to support notification with Safari 6, but only on Mac OSX 10.8+ (Mountain Lion).

+ +

See also

+ + diff --git a/files/zh-cn/web/api/notification/data/index.html b/files/zh-cn/web/api/notification/data/index.html new file mode 100644 index 0000000000..b7426d601e --- /dev/null +++ b/files/zh-cn/web/api/notification/data/index.html @@ -0,0 +1,165 @@ +--- +title: Notification.data +slug: Web/API/notification/data +translation_of: Web/API/Notification/data +--- +

{{APIRef("Web Notifications")}}

+ +

data 只读属性是 {{domxref("Notification")}} 的接口,当它作为构造函数的option可选项之一时,返回结构化的Notification的data数据。

+ +

当你创建Notification时,notification使用的数据可以使任意类型。

+ +

{{AvailableInWorkers}}

+ +

附加语法糖:关于克隆对象的速度研究。

+ +

https://dassur.ma/things/deep-copy/ 

+ +

博客作者认为目前(参考)最快的object克隆、复制方式。

+ +
function structuralClone(obj) {
+  return new Notification('', {data: obj, silent: true}).data;
+}
+ +

Syntax 表达式

+ +
var data = Notification.data;
+
+ +

Value(返回值)

+ +

结构化的克隆数据

+ +

Examples 例子

+ +

产生一个 notification; 简单的options 作为构造参数, 将会触发以option为构造参数的 Notification() .

+ +
var options = {
+  body: 'Do you like my body?',
+  data: 'I like peas.'
+}
+
+var n = new Notification('Test notification',options);
+
+n.data // should return 'I like peas.'
+ +

Specifications

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

Browser compatibility

+ +

{{ CompatibilityTable() }}

+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatChrome(44)}}{{CompatVersionUnknown}}{{ CompatNo() }}{{ CompatNo() }}{{CompatOpera(34)}}{{ CompatNo() }}
Available in workers{{CompatChrome(44)}}{{CompatUnknown}}{{CompatGeckoDesktop("41.0")}}{{CompatUnknown}}{{CompatOpera(34)}}{{CompatUnknown}}
Secure contexts only{{CompatChrome(62)}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatOpera(49)}}{{CompatUnknown}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroid WebviewChrome for AndroidEdgeFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari Mobile
Basic support{{ CompatNo() }}{{CompatChrome(44)}}{{CompatVersionUnknown}}{{ CompatNo() }}{{ CompatNo() }}{{ CompatNo() }}{{CompatOperaMobile(34)}}{{ CompatNo() }}
Available in workers{{ CompatNo() }}{{CompatChrome(44)}}{{CompatUnknown}}{{CompatGeckoMobile(41.0)}}{{CompatUnknown}}{{CompatUnknown}}{{CompatOperaMobile(34)}}{{CompatUnknown}}
Secure contexts only{{ CompatNo() }}{{CompatChrome(62)}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatOperaMobile(49)}}{{CompatUnknown}}
+
+ +

See also

+ + diff --git a/files/zh-cn/web/api/notification/dir/index.html b/files/zh-cn/web/api/notification/dir/index.html new file mode 100644 index 0000000000..51d64db69c --- /dev/null +++ b/files/zh-cn/web/api/notification/dir/index.html @@ -0,0 +1,167 @@ +--- +title: Notification.dir +slug: Web/API/notification/dir +translation_of: Web/API/Notification/dir +--- +

{{APIRef("Web Notifications")}}

+ +

{{domxref("Notification")}} 的`dir`是一个只读属性,它表示Notification中显示的文本方向 会{{domxref("Notification.Notification","Notification()")}} 构造函数里制定的`dir` 属性的值来设定。

+ +

{{AvailableInWorkers}}

+ +

语法

+ +
var direction = Notification.dir;
+
+ +

+ +

一个表示文本方向的{{domxref("DOMString")}}变量。 可能的取值为:

+ + + +
+

提示:似乎大多数浏览器都忽略了明确的LTR 和RTL 设置,要按照与浏览器的通用设置来操作。

+
+ +

例子

+ +

The following snippet fires a notification; a simple options object is created, then the notification is fired using the Notification() constructor.

+ +
var options = {
+  body: '你去过新疆吗?',
+  dir: 'rtl'
+}
+
+var n = new Notification('测试通知',options);
+
+n.dir // 应该返回 'rtl'
+
+ +

规则

+ + + + + + + + + + + + + + +
规则状态注释
{{SpecName('Web Notifications','#dom-notification-dir','dir')}}{{Spec2('Web Notifications')}}Living standard
+ +

浏览器兼容性

+ +

{{ CompatibilityTable() }}

+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
基础支持5 {{ property_prefix("webkit") }} (看提示)
+ 22
{{CompatVersionUnknown}}4.0 {{ property_prefix("moz") }} (看提示)
+ 22
{{ CompatNo() }}256 (看提示)
Available in workers{{CompatUnknown}}{{CompatUnknown}}{{CompatGeckoDesktop("41.0")}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidAndroid WebviewEdgeFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari MobileChrome for Android
基础支持{{ CompatUnknown() }} +

{{CompatVersionUnknown}}

+
{{CompatVersionUnknown}}4.0 {{ property_prefix("moz") }} (看注释)
+ 22
1.0.1 {{ property_prefix("moz") }} (看注释)
+ 1.2
{{ CompatNo() }}{{ CompatUnknown() }}{{ CompatNo() }} +

{{CompatVersionUnknown}}

+
能在Worker里调用{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatGeckoMobile(41.0)}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
+
+ +

Firefox OS 提示

+ +

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

+ +

Chrome 提示

+ +

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

+ +

Safari 提示

+ +

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

+ +

参见

+ + diff --git a/files/zh-cn/web/api/notification/icon/index.html b/files/zh-cn/web/api/notification/icon/index.html new file mode 100644 index 0000000000..40f76c90f0 --- /dev/null +++ b/files/zh-cn/web/api/notification/icon/index.html @@ -0,0 +1,63 @@ +--- +title: Notification.icon +slug: Web/API/notification/icon +tags: + - API + - Notification +translation_of: Web/API/Notification/icon +--- +

{{APIRef("Web Notifications")}}

+ +

{{domxref("Notification")}}的只读属性icon使得包含icon的 URL 被显示成通知的一部分,如同指定{{domxref("Notification.Notification","Notification()")}}构造函数中icon的属性。

+ +

{{AvailableInWorkers}}

+ +

语法

+ +
var icon = Notification.icon;
+
+ +

+ +

一个 {{domxref("USVString")}}。

+ +

示例

+ +

在此示例中,当我们想要发出一个通知时,我们运行一个简单的 spawnNotification() 函数——这是传递参数来指定我们想要的主体、图标和标题,然后它创建必要的options对象,并使用{{domxref("Notification.Notification","Notification()")}}构造函数来触发通知。

+ +
function spawnNotification(theBody,theIcon,theTitle) {
+  var options = {
+      body: theBody,
+      icon: theIcon
+  }
+  var n = new Notification(theTitle,options);
+}
+ +

Specifications

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

浏览器兼容性

+ + + +

{{Compat("api.Notification.icon")}}

+ +

参见

+ + diff --git a/files/zh-cn/web/api/notification/image/index.html b/files/zh-cn/web/api/notification/image/index.html new file mode 100644 index 0000000000..9a89fd402b --- /dev/null +++ b/files/zh-cn/web/api/notification/image/index.html @@ -0,0 +1,46 @@ +--- +title: Notification.image +slug: Web/API/notification/image +translation_of: Web/API/Notification/image +--- +

{{APIRef("Web Notifications")}}{{AvailableInWorkers}}{{securecontext_header}}

+ +

 image 是{{domxref("Notification")}} 接口的只读属性,包含了需要显示在通知信息里的图片的URL,可通过{{domxref("Notification.Notification","Notification()")}}构造函数的 image 选项指定。

+ +

语法

+ +
var image = Notification.image;
+
+ +

+ +

{{domxref("USVString")}}。

+ +

规范

+ + + + + + + + + + + + + + +
规范状态备注
{{SpecName('Web Notifications','#image-resource','image')}}{{Spec2('Web Notifications')}}Living standard
+ +

浏览器兼容性

+ + + +

{{Compat("api.Notification.image")}}

+ +

参见

+ + diff --git a/files/zh-cn/web/api/notification/index.html b/files/zh-cn/web/api/notification/index.html new file mode 100644 index 0000000000..f92c528b16 --- /dev/null +++ b/files/zh-cn/web/api/notification/index.html @@ -0,0 +1,264 @@ +--- +title: notification +slug: Web/API/notification +tags: + - API + - 参考 + - 通知 +translation_of: Web/API/Notification +--- +
{{APIRef("Web Notifications")}}
+ +
{{AvailableInWorkers}}
+ +
{{securecontext_header}}
+ +
+ +

Notifications API 的通知接口用于向用户配置和显示桌面通知。

+ +

构造方法

+ +
let notification = new Notification(title, options)
+
+ +

参数

+ +
+
title
+
一定会被显示的通知标题
+
options {{optional_inline}}
+
一个被允许用来设置通知的对象。它包含以下属性: +
    +
  • dir : 文字的方向;它的值可以是 auto(自动), ltr(从左到右), or rtl(从右到左)
  • +
  • lang: 指定通知中所使用的语言。这个字符串必须在 BCP 47 language tag 文档中是有效的。
  • +
  • body: 通知中额外显示的字符串
  • +
  • tag: 赋予通知一个ID,以便在必要的时候对通知进行刷新、替换或移除。
  • +
  • icon: 一个图片的URL,将被用于显示通知的图标。
  • +
+
+
+ +

属性

+ +

静态属性

+ +

这些属性仅在 Notification 对象上有效。

+ +
+
{{domxref("Notification.permission")}} {{readonlyinline}}
+
一个用于表明当前通知显示授权状态的字符串。可能的值包括:denied (用户拒绝了通知的显示), granted (用户允许了通知的显示), 或 default (因为不知道用户的选择,所以浏览器的行为与 denied 时相同).
+
+ +

实例属性

+ +

这些属性仅在 Notification 的实例中有效。

+ +
+
{{domxref("Notification.title")}} {{readonlyinline}} (moz only)
+
在构造方法中指定的 title 参数。
+
{{domxref("Notification.dir")}} {{readonlyinline}}
+
通知的文本显示方向。在构造方法的 options 中指定。
+
{{domxref("Notification.lang")}} {{readonlyinline}}
+
通知的语言。在构造方法的 options 中指定。
+
{{domxref("Notification.body")}} {{readonlyinline}}
+
通知的文本内容。在构造方法的 options 中指定。
+
{{domxref("Notification.tag")}} {{readonlyinline}}
+
通知的 ID。在构造方法的 options 中指定。
+
{{domxref("Notification.icon")}} {{readonlyinline}}
+
通知的图标图片的 URL 地址。在构造方法的 options 中指定。
+
+ +

事件处理

+ +
+
{{domxref("Notification.onclick")}}
+
处理 {{event("click")}} 事件的处理。每当用户点击通知时被触发。
+
{{domxref("Notification.onshow")}}
+
处理 {{event("show")}} 事件的处理。当通知显示的时候被触发。
+
{{domxref("Notification.onerror")}}
+
处理 {{event("error")}} 事件的处理。每当通知遇到错误时被触发。
+
{{domxref("Notification.onclose")}}
+
处理 {{event("close")}} 事件的处理。当用户关闭通知时被触发。
+
+ +

方法

+ +

静态方法

+ +

这些方法仅在 Notification 对象中有效。

+ +
+
{{domxref("Notification.requestPermission()")}}
+
用于当前页面向用户申请显示通知的权限。这个方法只能被用户行为调用(比如:onclick 事件),并且不能被其他的方式调用。
+
+ +

实例方法

+ +

这些方法仅在 Notification 实例或其 prototype 中有效。

+ +
+
{{domxref("Notification.close()")}}
+
用于关闭通知。
+
+ +

Notification 对象继承自 {{domxref("EventTarget")}} 接口。

+ +

{{page("/en-US/docs/Web/API/EventTarget","Methods")}}

+ +

Example

+ +

假定有如下 HTML:

+ +
<button onclick="notifyMe()">Notify me!</button>
+ +

接下来发送一条通知:

+ +
function notifyMe() {
+  // 先检查浏览器是否支持
+  if (!("Notification" in window)) {
+    alert("This browser does not support desktop notification");
+  }
+
+  // 检查用户是否同意接受通知
+  else if (Notification.permission === "granted") {
+    // If it's okay let's create a notification
+    var notification = new Notification("Hi there!");
+  }
+
+  // 否则我们需要向用户获取权限
+  else if (Notification.permission !== "denied") {
+    Notification.requestPermission().then(function (permission) {
+      // 如果用户接受权限,我们就可以发起一条消息
+      if (permission === "granted") {
+         var notification = new Notification("Hi there!");
+      }
+    });
+  }
+
+
+  // 最后,如果执行到这里,说明用户已经拒绝对相关通知进行授权
+  // 出于尊重,我们不应该再打扰他们了
+}
+ +

See the live result

+ +

{{ EmbedLiveSample('Example', '100%', 30) }}

+ +

规范

+ + + + + + + + + + + + + + + + +
规范状态备注
{{SpecName('Web Notifications')}}{{Spec2('Web Notifications')}}Initial specification.
+ +

权限

+ +

当你要在开放 web 应用中使用通知时,请确保将 desktop-notification 权限添加到你的 manifest 文件中。通知可以被用于任何权限级别,hosted 或更高。

+ +
"permissions": {
+    "desktop-notification":{}
+}
+ +

浏览器兼容性

+ +

{{ CompatibilityTable() }}

+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support5 {{ property_prefix("webkit") }} (see notes)
+ 22
4.0 {{ property_prefix("moz") }} (see notes)
+ 22
{{ CompatNo() }}256 (see notes)
+
+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari Mobile
Basic support{{ CompatUnknown() }}4.0 {{ property_prefix("moz") }} (see notes)
+ 22
1.0.1 {{ property_prefix("moz") }} (see notes)
+ 1.2
{{ CompatNo() }}{{ CompatUnknown() }}{{ CompatNo() }}
+
+ +

Gecko 备忘

+ + + +

Chrome 备忘

+ + + +

Safari 备忘

+ + + +

请参见

+ + diff --git a/files/zh-cn/web/api/notification/notification/index.html b/files/zh-cn/web/api/notification/notification/index.html new file mode 100644 index 0000000000..a4954e446a --- /dev/null +++ b/files/zh-cn/web/api/notification/notification/index.html @@ -0,0 +1,301 @@ +--- +title: Notification.Notification() +slug: Web/API/notification/Notification +tags: + - Notification.Notification() +translation_of: Web/API/Notification/Notification +--- +

{{APIRef("Web Notifications")}}

+ +

Notification() 构造函数创建一个新的{{domxref("Notification")}}对象实例。

+ +

{{AvailableInWorkers}}

+ +

语法

+ +
let myNotification = new Notification(title, options);
+ +

参数

+ +
+
title
+
定义一个通知的标题,当它被触发时,它将显示在通知窗口的顶部。
+
options {{optional_inline}}
+
options对象包含应用于通知的任何自定义设置选项。选项有: +
    +
  • dir: 显示通知的方向。默认是auto,跟随浏览器语言设置行为,你也可以通过设置ltr和rtl的值来覆盖该行为(虽然大多数浏览器似乎忽略这些设置)
  • +
  • lang: 通知的语言,如使用代表一个BCP 47语言标签的  {{domxref("DOMString")}} 指定的。请参阅Sitepoint ISO 2字母语言代码页面,以获得简单的参考。
  • +
  • badge: 一个 {{domxref("USVString")}} 包含用于表示通知的图像的URL, 当没有足够的空间来显示通知本身时。
  • +
  • body: 一个 {{domxref("DOMString")}} 表示通知的正文,将显示在标题下方。
  • +
  • tag:  一个 {{domxref("DOMString")}} 代表通知的 一个识别标签。
  • +
  • icon:  一个 {{domxref("USVString")}} 包含要在通知中显示的图标的URL。
  • +
  • image: 一个 {{domxref("USVSTring")}}包含要在通知中显示的图像的URL。
  • +
  • data: 您想要与通知相关联的任意数据。这可以是任何数据类型。
  • +
  • vibrate: 一个振动模式 vibration pattern 设备的振动硬件在通知触发时发出。
  • +
  • renotify: 一个 {{domxref("Boolean")}} 指定在新通知替换旧通知后是否应通知用户。默认值为false,这意味着它们不会被通知。
  • +
  • requireInteraction: 表示通知应保持有效,直到用户点击或关闭它,而不是自动关闭。默认值为false。
  • +
+ +

以下选项列在最新规范中,但在任何浏览器中都不支持. It is advisable to keep checking back regularly to see if the status of these has updated, and let us know if you find any out of date information.

+ +
    +
  • silent: 一个 {{domxref("Boolean")}} 指明通知是否应该是无声的,即,不需要发出声音或振动,无论设备设置如何。默认值为false,这意味着它不会保持静默。
  • +
  • sound:一个 {{domxref("USVString")}} 包含通知触发时要播放的音频文件的URL。
  • +
  • noscreen: 一个 {{domxref("Boolean")}} 指定通知触发是否应启用设备的屏幕。 默认值为false,这意味着它将启用屏幕。
  • +
  • sticky: 一个 {{domxref("Boolean")}} 指明通知是否应该是“粘”, 即不易被用户清理。默认值为false,这意味着它不会粘。
  • +
+ +

 

+
+
+ +

Example

+ +

In our Emogotchi demo (see source code), we run a simple spawnNotification() function when we want to fire a notification — this is passed arguments to specify the body, icon and title we want, then it creates the necessary options object and fires the notification using the Notification() constructor.

+ +
function spawnNotification(theBody,theIcon,theTitle) {
+  var options = {
+      body: theBody,
+      icon: theIcon
+  }
+  var n = new Notification(theTitle,options);
+
+}
+ +

Specifications

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName("Web Notifications","#dom-notification-notificationtitle-options","Notification()")}}{{Spec2('Web Notifications')}}Initial definition.
+ +

Browser compatibility

+ +

{{ CompatibilityTable() }}

+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support5 {{ property_prefix("webkit") }} (see notes)
+ 22
4.0 {{ property_prefix("moz") }} (see notes)
+ 22
{{ CompatNo() }}256 (see notes)
Available in workers{{CompatUnknown}}{{CompatGeckoDesktop("41.0")}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
icon option5 {{ property_prefix("webkit") }} (see notes)
+ 22
4.0 {{ property_prefix("moz") }} (see notes)
+ 22
{{ CompatNo() }}25{{ CompatNo() }}
vibrate{{CompatChrome(45.0)}}{{ CompatNo() }}{{ CompatNo() }}{{ CompatNo() }}{{ CompatNo() }}
requireInteraction{{CompatChrome(47.0)}}  32 
renotify{{CompatChrome(50.0)}}    
badge{{compatChrome(53.0)}}  {{CompatOpera(39.0)}} 
image{{compatChrome(55.0)}}  {{CompatUnknown}} 
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidAndroid WebviewFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari MobileChrome for Android
Basic support{{ CompatUnknown() }} +

{{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}}{{CompatGeckoMobile(41.0)}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
icon option{{ CompatUnknown() }}{{CompatVersionUnknown}}4.0 {{ property_prefix("moz") }} (see notes)
+ 22
1.0.1 {{ property_prefix("moz") }} (see notes)
+ 1.2
{{ CompatNo() }}{{ CompatUnknown() }}{{ CompatNo() }}{{CompatVersionUnknown}}
vibrate{{ CompatNo() }}{{CompatChrome(45.0)}}{{ CompatNo() }}{{ CompatNo() }}{{ CompatNo() }}32{{ CompatNo() }}{{CompatChrome(45.0)}}
requireInteraction{{CompatNo}}{{CompatNo}}     {{CompatNo}}
renotify{{CompatNo}}{{CompatNo}}     {{CompatChrome(50.0)}}
badge{{CompatNo}}{{compatChrome(53.0)}}   {{CompatOperaMobile(39.0)}} {{compatChrome(53.0)}}
image{{CompatNo}}{{CompatNo}}   {{CompatUnknown}} {{compatChrome(55.0)}}
+
+ +

Firefox OS notes

+ +

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

+ +

Chrome notes

+ +

Starting in Chrome 49, notifications do not work in incognito mode.

+ +

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

+ +

Safari notes

+ +

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

+ +

See also

+ + diff --git a/files/zh-cn/web/api/notification/onclick/index.html b/files/zh-cn/web/api/notification/onclick/index.html new file mode 100644 index 0000000000..7901b19faa --- /dev/null +++ b/files/zh-cn/web/api/notification/onclick/index.html @@ -0,0 +1,58 @@ +--- +title: Notification.onclick +slug: Web/API/notification/onclick +tags: + - Notification.onclick +translation_of: Web/API/Notification/onclick +--- +

{{APIRef("Web Notifications")}}

+ +

{{domxref("Notification")}} 接口的 onclick属性指定一个事件侦听器来接收 {{event("click")}} 事件。

+ +

当用户点击一个显示的{{domxref("Notification")}}时,会发生这些事件。

+ +

Syntax

+ +
Notification.onclick = function(event) { ... };
+
+ +

该方法的默认行为是将焦点移到与该通知相关联的 browsing context 的窗口. 如果你不希望这样, 可以在 event 对象上调用 preventDefault().

+ +

Examples

+ +

在下面这个例子中,我们使用 onclick 处理程序来监听点击通知的事件, 并在新窗口(通过包含一个参数'_blank')打开一个页面:

+ +
notification.onclick = function(event) {
+  event.preventDefault(); // prevent the browser from focusing the Notification's tab
+  window.open('http://www.mozilla.org', '_blank');
+}
+ +

Specifications

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

Browser compatibility

+ +

{{Page("/en-US/docs/Web/API/Notification","Browser compatibility")}}

+ +

See also

+ + diff --git a/files/zh-cn/web/api/notification/onclose/index.html b/files/zh-cn/web/api/notification/onclose/index.html new file mode 100644 index 0000000000..7c8577c85d --- /dev/null +++ b/files/zh-cn/web/api/notification/onclose/index.html @@ -0,0 +1,34 @@ +--- +title: Notification.onclose +slug: Web/API/notification/onclose +tags: + - Notification.onclose +translation_of: Web/API/Notification/onclose +--- +

{{APIRef("Web Notifications")}}

+ +

Summary

+ +

{{domxref("Notification")}}  接口的 onclose属性指定一个事件侦听器来接收 {{event("close")}}事件。

+ +

当一个{{domxref("Notification")}}关闭时,会发生这些事件。

+ +

Syntax

+ +
Notification.onclose = function() { ... };
+
+ +

Specifications

+ +

This event handler is no longer listed in the Notifications API spec.

+ +

Browser compatibility

+ +

{{Page("/en-US/docs/Web/API/Notification","Browser compatibility")}}

+ +

See also

+ + diff --git a/files/zh-cn/web/api/notification/onerror/index.html b/files/zh-cn/web/api/notification/onerror/index.html new file mode 100644 index 0000000000..6cf9d7295d --- /dev/null +++ b/files/zh-cn/web/api/notification/onerror/index.html @@ -0,0 +1,54 @@ +--- +title: Notification.onerror +slug: Web/API/notification/onerror +tags: + - Notification.onerror +translation_of: Web/API/Notification/onerror +--- +

{{APIRef("Web Notifications")}}

+ +

Summary

+ +

{{domxref("Notification")}}  接口的 onerror属性指定一个事件侦听器来接收 {{event("error")}} 事件。

+ +

当一个 {{domxref("Notification")}} 发生错误时,会发生这些事件(在许多情况下,一个错误阻止显示通知)。

+ +

 

+ +

Syntax

+ +
Notification.onerror = EventListener;
+ +

Value

+ +

A {{jsxref("function")}} which serves as the event handler for the {{event("error")}} event. When an error occurs, the specified function will be called. If null, no error handler is in effect.

+ +

Specifications

+ + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Web Notifications','#dom-notification-onerror','onerror')}}{{Spec2('Web Notifications')}}Initial specification.
+ +

Browser compatibility

+ +

{{Page("/en-US/docs/Web/API/Notification","Browser compatibility")}}

+ +

See also

+ + diff --git a/files/zh-cn/web/api/notification/onshow/index.html b/files/zh-cn/web/api/notification/onshow/index.html new file mode 100644 index 0000000000..0b3feb6fdc --- /dev/null +++ b/files/zh-cn/web/api/notification/onshow/index.html @@ -0,0 +1,34 @@ +--- +title: Notification.onshow +slug: Web/API/notification/onshow +tags: + - Notification.onshow +translation_of: Web/API/Notification/onshow +--- +

{{APIRef("Web Notifications")}}

+ +

Summary

+ +

{{domxref("Notification")}} 接口的onshow属性指定一个事件侦听器来接收 {{event("show")}}事件。

+ +

当一个 {{domxref("Notification")}} 显示时,会发生这些事件。

+ +

Syntax

+ +
Notification.onshow = function() { ... };
+
+ +

Specifications

+ +

This event handler is no longer listed in the Notifications API spec.

+ +

Browser compatibility

+ +

{{Page("/en-US/docs/Web/API/Notification","Browser compatibility")}}

+ +

See also

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

+ + diff --git a/files/zh-cn/web/api/notification/renotify/index.html b/files/zh-cn/web/api/notification/renotify/index.html new file mode 100644 index 0000000000..0f7620b6c0 --- /dev/null +++ b/files/zh-cn/web/api/notification/renotify/index.html @@ -0,0 +1,68 @@ +--- +title: Notification.renotify +slug: Web/API/notification/renotify +translation_of: Web/API/Notification/renotify +--- +
+
{{APIRef("Web Notifications")}}{{AvailableInWorkers}}{{securecontext_header}}
+ +

renotify 是 {{domxref("Notification")}} 接口的只读属性,如果有新的通知替换了一个旧的通知,这个属性指明用户是否应该重新收到通知。它也可以通过{{domxref("Notification.Notification","Notification()")}} 构造函数的 renotify option 来指定。

+
+ +

语法

+ +
var renotify = Notification.renotify;
+
+ +

+ +

{{domxref("Boolean")}}. 默认为false;设为 true 将会重新通知用户。

+ +

例子

+ +

以下代码片段用于在一个通知被替换以后触发通报以重新通知用户;它创建一个简单的 options 对象,然后使用Notification() 构造函数触发通报。

+ +
var options = {
+  body: 'Do you like my body?',
+  tag: 'renotify',
+  renotify: true
+}
+
+var n = new Notification('Test notification',options);
+
+n.renotify // should return true
+ +

使用注意

+ +

renotify 覆盖通知选项必须搭配 tag 标签选项进行使用,否则会收到错误通知。

+ +
Notifications which set the renotify flag must specify a non-empty tag.
+ +

规范

+ + + + + + + + + + + + + + +
规范状态备注
{{SpecName('Web Notifications','#dom-notification-renotify','renotify')}}{{Spec2('Web Notifications')}}Living standard
+ +

浏览器兼容性

+ + + +

{{Compat("api.Notification.renotify")}}

+ +

参见

+ + diff --git a/files/zh-cn/web/api/notification/requestpermission/index.html b/files/zh-cn/web/api/notification/requestpermission/index.html new file mode 100644 index 0000000000..90b3efebff --- /dev/null +++ b/files/zh-cn/web/api/notification/requestpermission/index.html @@ -0,0 +1,174 @@ +--- +title: Notification.requestPermission() +slug: Web/API/notification/requestPermission +tags: + - 通知 +translation_of: Web/API/Notification/requestPermission +--- +

{{APIRef("Web Notifications")}}

+ +

{{domxref("Notification")}} 接口的 requestPermission() 方法请求用户当前来源的权限以显示通知。

+ +

语法

+ +

最新的规范已将此方法更新为基于promise的语法,工作原理如下:

+ +
Notification.requestPermission().then(function(permission) { ... });
+ +

以前,语法是基于一个简单的回调;此版本现已弃用

+ +
Notification.requestPermission(callback);
+ +

参数

+ +
+
callback {{optional_inline}} {{deprecated_inline("gecko46")}}
+
一个可选的参数为权限请求的结果的回调函数。此参数已废弃,请使用Promise的语法。
+
+ +

返回值

+ +

一个 {{jsxref("Promise")}} ,将解析为一个 {{domxref("DOMString")}} ,它是用户对权限请求的选择。这个字符串可以是 granted(被授予), denied(被拒绝) 或者 default(默认)。

+ +

实例

+ +

下面这个代码片段将向用户请求权限,然后根据用户的不同选择,输出不同的日志。

+ +
Notification.requestPermission().then(function(result) {
+  if (result === 'denied') {
+    console.log('Permission wasn\'t granted. Allow a retry.');
+    return;
+  }
+  if (result === 'default') {
+    console.log('The permission request was dismissed.');
+    return;
+  }
+  // Do something with the granted permission.
+});
+ +

标准

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Web Notifications')}}{{Spec2('Web Notifications')}}Living standard
+ +

浏览器兼容性

+ +

{{CompatibilityTable}}

+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support5{{property_prefix("webkit")}}[1]
+ 22
{{CompatVersionUnknown}}{{CompatGeckoDesktop("2.0")}}{{property_prefix("moz")}}[2]
+ {{CompatGeckoDesktop("22.0")}}
{{CompatNo}}256[3]
promise-based version{{CompatChrome(46.0)}}{{CompatUnknown}}{{CompatGeckoDesktop("47.0")}}{{CompatUnknown}}{{CompatOpera(40)}}{{CompatNo}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidAndroid WebviewEdgeFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari MobileChrome for Android
Basic support{{CompatUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatGeckoMobile("2.0")}}{{property_prefix("moz")}}[2]
+ {{CompatGeckoMobile("22.0")}}
1.0.1{{property_prefix("moz")}}[2]
+ 1.2
{{CompatNo}}{{CompatUnknown}}{{CompatNo}}{{CompatVersionUnknown}}
promise-based version{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatGeckoMobile("47.0")}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
+
+ +

[1] 在 Chrome 22 之前,对于通知的支持请参考 old prefixed version of the specification 它使用 {{domxref("window.navigator.webkitNotifications","navigator.webkitNotifications")}} 对象去实例化一个新的通知。

+ +

在 Chrome 32 之前,不支持 {{domxref("Notification.permission")}} 。

+ +

在 Chrome 42 之前,不支持在 service worker 中使用这个API。

+ +

[2] 对于 Firefox 22 之前的版本 (Firefox OS <1.2),实例化一个新的通知必须使用 {{domxref("window.navigator.mozNotification", "navigator.mozNotification")}} 对象中的 createNotification 方法.

+ +

对于 Firefox 22 之前的版本 (Firefox OS <1.2),通知只会在 show 方法被调用后显示,而且只支持 click 和 close 事件。

+ +

Nick Desaulniers 写了一个 Notification shim 来同时兼容新旧两种写法。

+ +

在 Firefox OS 上有一个特殊的问题是:虽然你可以在通知中使用 包含路径的图标 ,但是如果应用被打包了,你就不能使用形如 /my_icon.png 这样的相对路径。当然你也不能使用window.location.origin + "/my_icon.png" ,因为 window.location.origin 在打包的应用中的值是nullmanifest origin field 修复了这个问题,但是它只能在 Firefox OS 1.1+ 中使用。一个潜在的支持 Firefox OS <1.1 的解决方案是 传递一个指向外部部署的图标的绝对路径的URL。这并不是一个理想的解决方案,因为这将导致通知以无图标的形式出现,然后图标才会被获取,但是这个方法适用于所有版本的 Firefox OS.

+ +

在 Firefox OS app 中使用通知的时候,确保添加 desktop-notification 权限到你的 manifest 文件中。通知即可在任何权限等级,外部部署或者像下面这样 "permissions": { "desktop-notification": {} } 的情况下使用。

+ +

[3] Safari 在 Safari 6 之后支持通知,但是只能在 Mac OSX 10.8+ (Mountain Lion) 中使用。

+ +

参见

+ + diff --git a/files/zh-cn/web/api/notification/requireinteraction/index.html b/files/zh-cn/web/api/notification/requireinteraction/index.html new file mode 100644 index 0000000000..a13a2eb100 --- /dev/null +++ b/files/zh-cn/web/api/notification/requireinteraction/index.html @@ -0,0 +1,59 @@ +--- +title: Notification.requireInteraction +slug: Web/API/notification/requireInteraction +tags: + - Notification + - 属性 + - 网页 +translation_of: Web/API/Notification/requireInteraction +--- +

{{APIRef("Web Notifications")}}{{AvailableInWorkers}}{{securecontext_header}}

+ +

{{domxref("Notification")}} 接口的 requireInteraction 属性是只读属性,它返回一个 {{jsxref("Boolean")}} (布尔值),指示在用户点击或关闭通知前,通知应该保持活动状态,而不是自动关闭。

+ +
+

注意: 此属性可以在创建通知时通过在 {{domxref("Notification.Notification()")}} 构造器的 options 参数接收的对象上设置 requireInteraction 属性为 true 来进行设置

+
+ +

语法

+ +
function spawnNotification(theTitle,theBody,shouldRequireInteraction) {
+  var options = {
+      body: theBody, //通知正文
+      requireInteraction: shouldRequireInteraction //在此处设置 requireInteraction
+  }
+  var n = new Notification(theTitle,options);
+}
+ +

+ +

{{jsxref("Boolean")}} (布尔值)。

+ +

规范

+ + + + + + + + + + + + + + +
规范状态意见
{{SpecName('Web Notifications','#dom-notification-requireinteraction','requireInteraction')}}{{Spec2('Web Notifications')}}现今标准
+ +

浏览器支持

+ + + +

{{Compat("api.Notification.requireInteraction")}}

+ +

相关内容

+ + diff --git a/files/zh-cn/web/api/notification/sound/index.html b/files/zh-cn/web/api/notification/sound/index.html new file mode 100644 index 0000000000..ffe90b4955 --- /dev/null +++ b/files/zh-cn/web/api/notification/sound/index.html @@ -0,0 +1,129 @@ +--- +title: Notification.sound +slug: Web/API/notification/sound +translation_of: Web/API/notification/sound +--- +

{{APIRef("Web Notifications")}}

+ +
+

Note: 这个属性并没有完全被一些浏览器支持.

+
+ +

 sound 是 {{domxref("Notification")}}的只读属性,interface specifies the URL of an audio file to be played when the notification fires. This is specified in the sound option of the {{domxref("Notification.Notification","Notification()")}} constructor.

+ +

Syntax

+ +
var sound = Notification.sound;
+
+ +

Value

+ +

A {{domxref("USVString")}}.

+ +

Examples

+ +

The following snippet is intended to fire a sound along with the notification; a simple options object is created, then the notification is fired using the Notification() constructor.

+ +
var options = {
+  body: 'Do you like my body?',
+  sound: 'audio/alert.mp3'
+}
+
+var n = new Notification('Test notification',options);
+
+n.sound // should return 'audio/alert.mp3'
+ +

Specifications

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

Browser compatibility

+ +

{{ CompatibilityTable() }}

+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{ CompatNo() }}{{ CompatNo() }}{{ CompatNo() }}{{ CompatNo() }}{{ CompatNo() }}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidAndroid WebviewFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari MobileChrome for Android
Basic support{{ CompatNo() }} +

{{ CompatNo() }}

+
{{ CompatNo() }}{{ CompatNo() }}{{ CompatNo() }}{{ CompatNo() }}{{ CompatNo() }} +

{{ CompatNo() }}

+
+
+ +

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

+ + diff --git a/files/zh-cn/web/api/notification/using_web_notifications/index.html b/files/zh-cn/web/api/notification/using_web_notifications/index.html new file mode 100644 index 0000000000..40bbb3848b --- /dev/null +++ b/files/zh-cn/web/api/notification/using_web_notifications/index.html @@ -0,0 +1,292 @@ +--- +title: 使用 Web Notifications +slug: Web/API/notification/Using_Web_Notifications +tags: + - Firefox OS + - Notifications + - Using the Notifications API + - 通知 +translation_of: Web/API/Notifications_API/Using_the_Notifications_API +--- +

{{APIRef("Web Notifications")}}

+ +

Notifications API 允许网页或应用程序在系统级别发送在页面外部显示的通知;这样即使应用程序空闲或在后台,Web应用程序也会向用户发送信息。本文将介绍在您自己的应用程序中使用此API的基础知识。

+ +

{{AvailableInWorkers}}

+ +

通常,系统通知是指操作系统的标准通知机制,例如思考典型的桌面系统或移动设备如何发布通知。

+ +

                             

+ +

                        

+ +

系统通知系统当然会因平台和浏览器而异,但无需担心,通知API被编写为通用的,足以与大多数系统通知系统兼容。

+ +

Web Notifications API 使页面可以发出通知,通知将被显示在页面之外的系统层面上(通常使用操作系统的标准通知机制,但是在不同的平台和浏览器上的表现会有差异)。这个功能使  web 应用可以向用户发送信息,即使应用处于空闲状态。最明显的用例之一是一个网页版电子邮件应用程序,每当用户收到了一封新的电子邮件都需要通知用户,即使用户正在使用另一个应用程序。

+ +

要显示一条通知,你需要先请求适当的权限,然后你可以实例化一个 {{domxref("Notification")}} 实例:

+ +
Notification.requestPermission( function(status) {
+  console.log(status); // 仅当值为 "granted" 时显示通知
+  var n = new Notification("title", {body: "notification body"}); // 显示通知
+});
+
+ +

请求权限

+ +

在应用可以发送通知之前,用户必须授予应用有权这么做。这是一个常见的要求,当一个 API 至少一次试图与 web 页外部进行交互时,用户不得不专门授予该应用程序有权限提出通知,从而让用户控制允许哪些应用程序或网站显示通知。

+ +

检查当前权限状态

+ +

你可以通过检查只读属性 {{domxref("Notification.permission")}} 的值来查看你是否已经有权限。该属性的值将会是下列三个之一:

+ +
+
default
+
用户还未被询问是否授权,所以通知不会被显示。参看 {{anch("Getting permission")}} 以了解如何请求显示通知的权限。
+
granted
+
表示之前已经询问过用户,并且用户已经授予了显示通知的权限。
+
denied
+
用户已经明确的拒绝了显示通知的权限。
+
+ +
+

注:Safari 和 Chrome (在 32 版本之前) 还没有实现 permission 属性。

+
+ +

获得权限

+ +

如果权限尚未被授予,那么应用不得不通过 {{domxref("Notification.requestPermission()")}} 方法让用户进行选择。这个方法接受一个回调函数,一旦用户回应了显示通知的请求,将会调用这个函数。

+ +

通常你应在你的应用首次初始化的时候请求显示通知的权限:

+ +
window.addEventListener('load', function () {
+  Notification.requestPermission(function (status) {
+    // 这将使我们能在 Chrome/Safari 中使用 Notification.permission
+    if (Notification.permission !== status) {
+      Notification.permission = status;
+    }
+  });
+});
+ +
+

注:Chrome 不允许你在 load 事件处理中调用 {{domxref("Notification.requestPermission()")}} (参见 issue 274284)。

+
+ +

Notification API 的清单权限

+ +

请注意 Notification API 不是 {{Glossary("privileged")}} 或 {{Glossary("certified")}},因此当你在一个开放 web 应用中使用它时,你仍需要在你的 manifest.webapp 文件中包含以下项目:

+ +
"permissions": {
+  "desktop-notification": {
+    "description": "Needed for creating system notifications."
+  }
+}
+ +
+

注:当安装应用程序时,你不需要通过上面的代码显式的请求权限,但您仍然需要在触发通知之前取得权限项。

+
+ +

创建通知

+ +

创建通知很简单,只需要用 {{domxref("Notification")}} 构造方法。这个构造函数需要一个用来显示在通知内的标题以及一些用来增强通知的选项,例如 {{domxref("Notification.icon","icon")}} 或文本 {{domxref("Notification.body","body")}}。

+ +

一旦通知被创建出来,它会立即被显示出来。为了跟踪通知当前的状态,在 {{domxref("Notification")}} 实例层面上会有4个事件被触发:

+ +
+
{{event("show")}}
+
当通知被显示给用户时触发。
+
{{event("click")}}
+
当用户点击通知时触发。
+
{{event("close")}}
+
当通知被关闭时触发。
+
{{event("error")}}
+
当通知发生错误的时候触发。这通常是因为通知由于某些原因而无法显示。
+
+ +

这些事件可以通过事件处理跟踪 {{domxref("Notification.onshow","onshow")}}、{{domxref("Notification.onclick","onclick")}}、{{domxref("Notification.onclose","onclose")}} 和 {{domxref("Notification.onerror","onerror")}}。因为 {{domxref("Notification")}} 同样继承自 {{domxref("EventTarget")}},因此可以对它调用 {{domxref("EventTarget.addEventListener","addEventListener()")}} 方法。

+ +
+

注:Firefox 和 Safari 会在一定时间后自动关闭通知(大约4秒)。这也会发生在操作系统层面。

+ +

当然你也可以通过代码做到,调用 {{domxref("Notification.close()")}} 方法,就像下面的代码一样:

+ +
var n = new Notification("Hi!");
+n.onshow = function () {
+  setTimeout(n.close.bind(n), 5000);
+}
+
+ +

当你接收到一个“close”事件时,并不能保证这个通知是被用户关闭的。这是符合规范的,其中指出:“当一个通知被关闭时,通知的关闭动作都必须执行,不论是底层通知平台导致,还是用户导致。”

+
+ +

简单的例子

+ +

假定有如下的 HTML:

+ +
<button>Notify me!</button>
+ +

它可能通过这样的方式处理通知:

+ +
window.addEventListener('load', function () {
+  // 首先,让我们检查我们是否有权限发出通知
+  // 如果没有,我们就请求获得权限
+  if (window.Notification && Notification.permission !== "granted") {
+    Notification.requestPermission(function (status) {
+      if (Notification.permission !== status) {
+        Notification.permission = status;
+      }
+    });
+  }
+
+  var button = document.getElementsByTagName('button')[0];
+
+  button.addEventListener('click', function () {
+    // 如果用户同意就创建一个通知
+    if (window.Notification && Notification.permission === "granted") {
+      var n = new Notification("Hi!");
+    }
+
+    // 如果用户没有选择是否显示通知
+    // 注:因为在 Chrome 中我们无法确定 permission 属性是否有值,因此
+    // 检查该属性的值是否是 "default" 是不安全的。
+    else if (window.Notification && Notification.permission !== "denied") {
+      Notification.requestPermission(function (status) {
+        if (Notification.permission !== status) {
+          Notification.permission = status;
+        }
+
+        // 如果用户同意了
+        if (status === "granted") {
+          var n = new Notification("Hi!");
+        }
+
+        // 否则,我们可以让步的使用常规模态的 alert
+        else {
+          alert("Hi!");
+        }
+      });
+    }
+
+    // 如果用户拒绝接受通知
+    else {
+      // 我们可以让步的使用常规模态的 alert
+      alert("Hi!");
+    }
+  });
+});
+ +

这是实际的结果:

+ +

{{ EmbedLiveSample('Simple_example', '100%', 30) }}

+ +

处理重复的通知

+ +

某些情况下对于用户来说,显示大量通知是件令人痛苦的事情。比如,如果一个即时通信应用向用户提示每一条传入的消息。为了避免数以百计的不必要通知铺满用户的桌面,可能需要接管一个挂起消息的队列。

+ +

因此,需要为新建的通知添加一个标记。如果有一条通知也具有一个相同的标记,并且还没有被显示,那么这条新通知将会替换上一条通知。如果有一条通知具有一个相同的标记,并且已经显示出来了,那么上一条通知将会被关闭,新通知将会被显示出来。

+ +

使用标记的例子

+ +

假定有如下 HTML:

+ +
<button>Notify me!</button>
+ +

它有可能通过这种方式处理的多个通知:

+ +
window.addEventListener('load', function () {
+  // 首先,我们检查是否具有权限显示通知
+  // 如果没有,我们就申请权限
+  if (window.Notification && Notification.permission !== "granted") {
+    Notification.requestPermission(function (status) {
+      if (Notification.permission !== status) {
+        Notification.permission = status;
+      }
+    });
+  }
+
+  var button = document.getElementsByTagName('button')[0];
+
+  button.addEventListener('click', function () {
+    // 如果用户同意接收通知,我们就尝试发送10条通知
+    if (window.Notification && Notification.permission === "granted") {
+      for (var i = 0; i < 10; i++) {
+        // 感谢标记,我们应该只看到内容为 "Hi! 9" 的通知
+        var n = new Notification("Hi! " + i, {tag: 'soManyNotification'});
+      }
+    }
+
+    // 如果用户没有选择是否同意显示通知
+    // 注:由于在 Chrome 中不能确定 permission 属性是否有值,因此检查
+    // 该属性值是否为 "default" 是不安全的。
+    else if (window.Notification && Notification.permission !== "denied") {
+      Notification.requestPermission(function (status) {
+        if (Notification.permission !== status) {
+          Notification.permission = status;
+        }
+
+        // 如果用户同意
+        if (status === "granted") {
+          for (var i = 0; i < 10; i++) {
+            // Thanks to the tag, we should only see the "Hi! 9" notification
+            var n = new Notification("Hi! " + i, {tag: 'soManyNotification'});
+          }
+        }
+
+        // 否则改用 alert
+        else {
+          alert("Hi!");
+        }
+      });
+    }
+
+    // 如果用户拒绝
+    else {
+      // 改用 alert
+      alert("Hi!");
+    }
+  });
+});
+ +

实际效果如下:

+ +

{{ EmbedLiveSample('.E5.A4.84.E7.90.86.E9.87.8D.E5.A4.8D.E7.9A.84.E9.80.9A.E7.9F.A5', '100%', 30) }}

+ +

接收点击应用通知的通知

+ +

当用户点击一个由应用产生的通知时,视情况而定,你将会有两种方式被告知点击事件发生了:

+ +
    +
  1. 如果你的程序没有被关闭或转入后台,那么在你会收到一个点击事件。
  2. +
  3. 其他情况下会收到一条系统消息。
  4. +
+ +

参考 这个代码片段 作为例子,展示了如何处理。

+ +

规范

+ + + + + + + + + + + + + + + + +
规范状态备注
{{SpecName('Web Notifications')}}{{Spec2('Web Notifications')}}Initial specification.
+ +

浏览器兼容性

+ +

{{page("/zh-CN/Web/API/Notification",".E6.B5.8F.E8.A7.88.E5.99.A8.E5.85.BC.E5.AE.B9.E6.80.A7")}}

+ +

参考

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