注意: 此特征可用于 Web 工作者 .

安全上下文
此特征只可用于 安全上下文 (HTTPS),在某些或所有 支持浏览器 .

permission 只读特性在 Notification interface indicates the current permission granted by the user for the current origin to display web notifications.

句法

var permission = Notification.permission;
					

A DOMString representing the current permission. The value can be:

  • granted : The user has explicitly granted permission for the current origin to display system notifications.
  • denied : The user has explicitly denied permission for the current origin to display system notifications.
  • default : The user decision is unknown; in this case the application will act as if permission was denied .

范例

The following snippet could be used 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)) {
    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.
}
					

规范

规范 状态 注释
Notifications API
The definition of 'permission' in that specification.
实时标准 Living standard

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
permission Chrome Yes Edge 14 Firefox Yes IE No Opera Yes Safari Yes WebView Android No Chrome Android Yes Firefox Android Yes Opera Android Yes Safari iOS No Samsung Internet Android Yes

图例

完整支持

完整支持

不支持

不支持

另请参阅

元数据

  • 最后修改: