--- title: Notification slug: Web/API/notification tags: - 待翻譯 translation_of: Web/API/Notification ---
{{APIRef("Web Notifications")}}
Notifications API 的 Notification
interface 是用來設置及顯示「桌面通知」給使用。
{{AvailableInWorkers}}
以下的屬性僅適用於 Notification
實例。
以下屬應僅適用於 Notification
實例。
The following properties are listed in the most up-to-date spec, but are not supported in any browsers yet. 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.
底下這些列在 {{anch("browser compatibility")}} 中的 event handlers 雖然還有支援,但已經從近期的 spec 中移除了。因為瀏覽器會在未來的版本逐漸停止支援,最好還是不要使用它們。
這些方法只能在 Notification 實例上使用。
These properties are available only on an instance of the Notification
object or through its prototype
. The Notification
object also inherits from the {{domxref("EventTarget")}} interface.
先看這段基本的 HTML:
<button onclick="notifyMe()">Notify me!</button>
用下面的程式碼我們可以送出通知: 底下的程式碼雖然略嫌冗長,但完整的示範了如何在送出通知之前,事先檢查了瀏覽器是否支援 Notification、使用者是否授權這個網域的網頁送出通知,以及在需要的時候向使用者詢問是否開放權限。
function notifyMe() { // 首先讓我們確定瀏覽器支援 Notification if (!("Notification" in window)) { alert("這個瀏覽器不支援 Notification"); } // 再檢查使用者是否已經授權執行 Notification else if (Notification.permission === "granted") { // 如果已經授權就可以直接新增 Notification 了! var notification = new Notification("安安你好!"); } // 否則,我們會需要詢問使用者是否開放權限 else if (Notification.permission !== 'denied') { Notification.requestPermission(function (permission) { // 如果使用者同意了就來新增一個 Notification 打聲招呼吧 if (permission === "granted") { var notification = new Notification("安安~"); } }); } // 如果使用者還是不同意授權執行 Notification // 最好還是進行適當的處理以避免繼續打擾使用者 }
{{EmbedLiveSample('範例', '100%', 30)}}
在很多時候,你應該不會想要這麼冗長的程式碼。 比如說,在我們的 Emogotchi demo (see source code) 之中,我們只寫了 {{domxref("Notification.requestPermission")}} 而不用進一步檢查是否已經獲得了權限:
Notification.requestPermission();
然後我們只需要在要新增 Notfication 時執行這個 spawnNotification()
— 透過傳入指定的 body、icon、title,然後它就會產生我們所需的 options 參數並透過 {{domxref("Notification.Notification","Notification()")}} 建構子發送 Notification.
function spawnNotification(theBody,theIcon,theTitle) { var options = { body: theBody, icon: theIcon } var n = new Notification(theTitle,options); }
Specification | Status | Comment |
---|---|---|
{{SpecName('Web Notifications')}} | {{Spec2('Web Notifications')}} | Living standard |
{{CompatibilityTable}}
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | 5{{property_prefix("webkit")}}[1] 22 |
4.0 {{property_prefix("moz")}}[2] 22 |
{{CompatNo}} | 25 | 6[3] |
icon |
5{{property_prefix("webkit")}}[1] 22 |
4.0 {{property_prefix("moz")}}[2] 22 |
{{CompatNo}} | 25 | {{CompatNo}} |
Available in workers | {{CompatUnknown}} | {{CompatGeckoDesktop("41.0")}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} |
silent |
{{CompatChrome(43.0)}} | {{CompatNo}} | {{CompatNo}} | {{CompatNo}} | {{CompatNo}} |
noscreen , renotify , sound , sticky |
{{CompatNo}} | {{CompatNo}} | {{CompatNo}} | {{CompatNo}} | {{CompatNo}} |
Feature | Android | Android Webview | Firefox Mobile (Gecko) | Firefox OS | IE Mobile | Opera Mobile | Safari Mobile | Chrome for Android |
---|---|---|---|---|---|---|---|---|
Basic support | {{CompatUnknown}} |
{{CompatVersionUnknown}} |
4.0{{property_prefix("moz")}}[2] 22 |
1.0.1{{property_prefix("moz")}}[2] 1.2 |
{{CompatNo}} | {{CompatUnknown}} | {{CompatNo}} |
{{CompatVersionUnknown}} |
icon |
{{CompatUnknown}} | {{CompatVersionUnknown}} | 4.0{{property_prefix("moz")}}[2] 22 |
1.0.1{{property_prefix("moz")}}[2] 1.2 |
{{CompatNo}} | {{CompatUnknown}} | {{CompatNo}} | {{CompatVersionUnknown}} |
Available in workers | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatGeckoMobile("41.0")}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} |
silent |
{{CompatNo}} | {{CompatChrome(43.0)}} | {{CompatNo}} | {{CompatNo}} | {{CompatNo}} | {{CompatNo}} | {{CompatNo}} | {{CompatChrome(43.0)}} |
noscreen , renotify , sound , sticky |
{{CompatNo}} | {{CompatNo}} | {{CompatNo}} | {{CompatNo}} | {{CompatNo}} | {{CompatNo}} | {{CompatNo}} | {{CompatNo}} |
[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).