Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. Notification
  4. Notification()

Notification: Notification() constructor

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

Secure context: This feature is available only insecure contexts (HTTPS), in some or allsupporting browsers.

Note: This feature is available inWeb Workers.

TheNotification() constructor creates a newNotification object instance, which represents a user notification.

Trying to create a notification inside theServiceWorkerGlobalScope using theNotification() constructor will throw aTypeError. UseServiceWorkerRegistration.showNotification() instead.

You must first get permission before being able to display notifications, usingNotification.requestPermission(). The permission may not be grantable, for example if the page is in private browsing mode.

This constructor throws aTypeError when called in nearly all mobile browsers and this is unlikely to change, because web pages on mobile devices almost never "run in the background", which is the main use case for notifications. Instead, you need to register a service worker and useServiceWorkerRegistration.showNotification(). SeeChrome issue for more information.

Syntax

js
new Notification(title)new Notification(title, options)

Parameters

title

Defines a title for the notification, which is shown at the top of the notification window.

optionsOptional

An options object containing any custom settings that you want to apply to the notification. The possible options are:

actionsOptional

Must be unspecified or an empty array.actions is only supported for persistent notifications fired from a service worker usingServiceWorkerRegistration.showNotification().

badgeOptional

A string containing the URL of the image used to represent the notification when there isn't enough space to display the notification itself; for example, the Android Notification Bar. On Android devices, the badge should accommodate devices up to 4x resolution, about 96x96px, and the image will be automatically masked.

bodyOptional

A string representing the body text of the notification, which is displayed below the title. The default is the empty string.

dataOptional

Arbitrary data that you want associated with the notification. This can be of anystructured-clonable data type. The default isnull.

dirOptional

The direction in which to display the notification. It defaults toauto, which just adopts the browser's language setting behavior, but you can override that behavior by setting values ofltr andrtl (although most browsers seem to ignore these settings.)

iconOptional

A string containing the URL of an icon to be displayed in the notification.

imageOptional

A string containing the URL of an image to be displayed in the notification.

langOptional

The notification's language, as specified using a string representing aBCP 47 language tag. The default is the empty string.

renotifyOptional

A boolean value specifying whether the user should be notified after a new notification replaces an old one. The default isfalse, which means they won't be notified. Iftrue, thentag also must be set.

requireInteractionOptional

Indicates that a notification should remain active until the user clicks or dismisses it, rather than closing automatically. The default value isfalse.

silentOptional

A boolean value specifying whether the notification should be silent, i.e., no sounds or vibrations should be issued regardless of the device settings. If set totrue, the notification is silent; if set tonull (the default value), the device's default settings are respected.

tagOptional

A string representing an identifying tag for the notification. The default is the empty string.

timestampOptional

A timestamp, given asUnix time in milliseconds, representing the time associated with the notification. This could be in the past when a notification is used for a message that couldn't immediately be delivered because the device was offline, or in the future for a meeting that is about to start.

vibrateOptional

Avibration pattern for the device's vibration hardware to emit with the notification. If specified,silent must not betrue.

Return value

An instance of theNotification object.

Exceptions

TypeError

Thrown if:

  • The constructor is called within theServiceWorkerGlobalScope.
  • Theactions option is specified and is not empty.
  • Thesilent option istrue and thevibrate option is specified.
  • Therenotify option istrue but thetag option is empty.
DataCloneErrorDOMException

Thrown if serializing thedata option failed for some reason.

Examples

Here is a most basic example to only show a notification if permission is already granted. For more complete examples, see theNotification page.

js
if (Notification.permission === "granted") {  const notification = new Notification("Hi there!");}

Specifications

Specification
Notifications API
# dom-notification-notification

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp