--- title: Notification slug: Web/API/notification translation_of: Web/API/Notification ---
Wenn Sie notifications in einer offenen web app verwenden, fügen sie die desktop-notification permission zu ihrem manifest file hinzu. Notifications können für jedes permission level, hosted oder darüber verwendet werden.
"permissions": {
"desktop-notification":{}
}
var notification = new Notification(title, options);
title
options
{{optional_inline}}dir
: Die Ausrichtung des Textes; Verfügbar sind auto
, ltr
, oder rtl
.lang
: Spezifiziere die verwendete Sprache. Dieser String muss ein valides BCP 47 language tag sein.body
: Ein String, welcher jeglichen extra Inhalt einer notification beinhaltet.tag
: Die ID einer gegebene notification, um diese abzurufen, zu löschen, zu ersetzen oder zu löschen. icon
: Die Url für das verwendete Icon in einer notification.data
: Ein Benutzerdefiniertes Datenfeld.Diese Eigenschaften sind nur im Notification-Objekt selbst verfügbar.
Diese Eigenschaften sind nur in Instanzen des Notification-Objekts verfügbar.
These methods are available only on the Notification
object itself.
These properties are available only on an instance of the Notification
object or through its prototype
. The Notification
objects also inherit from the {{domxref("EventTarget")}} interface.
{{page("/en-US/docs/Web/API/EventTarget","Methods")}}
Assume this basic HTML:
<button onclick="notifyMe()">Notify me!</button>
It's possible to send a notification as follows:
function notifyMe() { // Let's check if the browser supports notifications if (!("Notification" in window)) { alert("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.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. }
{{ EmbedLiveSample('Example', '100%', 30) }}
Specification | Status | Comment |
---|---|---|
{{SpecName('Web Notifications')}} | {{Spec2('Web Notifications')}} | Initial specification. |
{{ CompatibilityTable() }}
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | 5 {{ property_prefix("webkit") }} (see notes) 22 |
4.0 {{ property_prefix("moz") }} (see notes) 22 |
{{ CompatNo() }} | 25 | 6 (see notes) |
Feature | Android | Android Webview | Firefox Mobile (Gecko) | Firefox OS | IE Mobile | Opera Mobile | Safari Mobile | Chrome for Mobile |
---|---|---|---|---|---|---|---|---|
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}} |
createNotification
method.show
method and was supporting the click
and close
events only./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 with the icon missing, then the icon is fetched, but it works on all versions of Firefox OS.{{ref("1")}} Deprecated since Android 4.0.