Notification.permission - Web API 接口参考 编辑

Notification 的只读属性 permission 用来表明用户是否允许当前域显示Web Notification. 

Note: 此特性在 Web Worker 中可用。

Syntax

var permission = Notification.permission;

Value

permission 的类型为 DOMString . 该属性的可能值为:

  • granted: 用户已经明确的授予了显示通知的权限。.
  • denied: 用户已经明确的拒绝了显示通知的权限。
  • default: 用户还未被询问是否授权; 这种情况下权限将视为 denied.

Examples

下面的代码片段详细的说明了,当你首次检查浏览器是否支持Notification,然后检查当前域是否被授予了发送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.
}

Specifications

SpecificationStatusComment
Notifications API
permission
Living StandardLiving standard

Browser compatibility

We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains. Find out how you can help!
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support5 webkit (see notes)
22
(Yes)4.0 moz (see notes)
22
未实现256 (see notes)
Available in workers??41.0 (41.0)???
FeatureAndroidAndroid WebviewEdgeFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari MobileChrome for Android
Basic support?

(Yes)

(Yes)4.0 moz (see notes)
22
1.0.1 moz (see notes)
1.2
未实现?未实现

(Yes)

Available in workers???41.0 (41.0)?????

Firefox OS notes

Chrome notes

Safari notes

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:73 次

字数:5964

最后编辑:7 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文