Duke Yin's Technology database

Web notifications

Examples

Assume this basic HTML:

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

It’s possible to send a notification as follows — here we present a fairly verbose and complete set of code you could use if you wanted to first check whether notifications are supported, then check if permission has been granted for the current origin to send notifications, then request permission if required, before then sending a notification.

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 already 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().then(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.
}

Chrome and Firefox no longer allow notification permissions to be requested from cross-origin <iframe>s, with other browsers to follow.


Note: This feature is available in Web Workers.

Secure context
This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

The Notification interface of the Notifications API is used to configure and display desktop notifications to the user. These notifications’ appearance and specific functionality vary across platforms but generally they provide a way to asynchronously provide information to the user.

Constructor

Notification()Creates a new instance of the Notification object.

Properties

Static properties

These properties are available only on the Notification object itself.

Notification.permission Read only A string representing the current permission to display notifications. Possible values are:

  • denied — The user refuses to have notifications displayed.
  • granted — The user accepts having notifications displayed.
  • default — The user choice is unknown and therefore the browser will act as if the value were denied.

Notification.maxActions Read only 

Instance properties

These properties are available only on instances of the Notification object.

Notification.actions Read only The actions array of the notification as specified in the constructor’s optionsparameter.

Notification.badge Read only The URL of the image used to represent the notification when there is not enough space to display the notification itself.

Notification.body Read only The body string of the notification as specified in the constructor’s optionetersparameter.

Notification.data Read only Returns a structured clone of the notification’s data.

Notification.dir Read only The text direction of the notification as specified in the constructor’s optionspara

meter.Notification.lang Read only The language code of the notification as specified in the constructor’s optionsparameter.

Notification.tag Read only The ID of the notification (if any) as specified in the constructor’s options parameter.

Notification.icon Read only The URL of the image used as an icon of the notification as specified in the constructor’s options parameter.

Notification.image Read only The URL of an image to be displayed as part of the notification, as specified in the constructor’s options parameter.

Notification.renotify Read only

Specifies whether the user should be notified after a new notification replaces an old one.

Notification.requireInteraction Read only

A Boolean indicating that a notification should remain active until the user clicks or dismisses it, rather than closing automatically.

Notification.silent Read only Specifies whether the notification should be silent — i.e., no sounds or vibrations should be issued, regardless of the device settings.

Notification.timestamp Read only Specifies the time at which a notification is created or applicable (past, present, or future).

Notification.title Read only The title of the notification as specified in the first parameter of the constructor.

Notification.vibrate Read only Specifies a vibration pattern for devices with vibration hardware to emit.

Event handlers

Notification.onclickA handler for the click event. It is triggered each time the user clicks on the notification.

Notification.oncloseA handler for the close event. It is triggered when the user closes the notification.

Notification.onerrorA handler for the error event. It is triggered each time the notification encounters an error.

Notification.onshowA handler for the show event. It is triggered when the notification is displayed.

Methods

Static methods

These methods are available only on the Notification object itself.

Notification.requestPermission()

Requests permission from the user to display notifications.

Instance methods

These properties are available only on an instance of the Notification object or through its prototype. The Notificationobject also inherits from the EventTargetinterface.

Notification.close()Programmatically closes a notification instance.

引用关闭,但你能发布评论.

评论

标注 * 的为必填项。