HTML5 桌面通知

Notification

Properties

title:"标题:"body:"内容"icon:"图片地址"C/em%3>tag:"1"// 通知框ID,相同id可替换,而不是出现新的通知框lang:""// 语言dir:"auto"// 文字方向

new Notification('别动神仙说:', {
  body: '这里是body',
  icon: 'http://q4.qlogo.cn/g?b=qq&k=icUjVAN5Ja7BCDQ1ICl8Svw&s=40',
  tag: 1
});

onshow: null // 显示通知框时调用onclick: null // 点击通知框时调用onclose: null // 点击通知框关闭按钮时调用onerror: null

例如实现通知弹出一段时间后自动关闭

var notification = new Notification('标题');
notification.onshow = function () {
  setTimeout(function () {
    notification.close();
  }, 3000);
}

Notification.permission 有三种状态

  • default:未设置过为这个状态,通过Notification.requestPermission()可以询问用户是否允许通知
  • granted:用户点击允许后的状态
  • denied: 用户点击拒绝后的状态,通知框不可用

Methods

Notification.requestPermission()popover requesting for the user’s permission 一般在Notification.permission === 'default'时,用户通过点击等操作调用

document.onclick = function() {
  Notification.requestPermission()
}
使用回调

Notification.requestPermission(function (permission) {
  // 可在确认后直接弹出
  if (permission === 'granted') {
    var notification = new Notification('弹窗');
  }
});

Notification.close() 通知框关闭

function notify() {
  if (!("Notification"in window)) {
    alert("This browser does not support desktop notification");
    return;
  }

  if (Notification.permission ==="granted") {
    var notification = new Notification("Hi there!");
  }
  else if (Notification.permission === 'default') {
    Notification.requestPermission(function (permission) {
      if (permission ==="granted") {
        var notification = new Notification("Hi there!");
      }
    });
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值