使用Notifation显示桌面通知

使用Chrome的时候有时我们会收到这样的提示:
这里写图片描述

之后就可能在右下角弹出类似提示框一样的通知,但是与普通的提示框比如jquery-ui实现的不同的是,这个提示框不属于DOM元素,而是属于桌面系统的元素,不依赖与浏览器的位置,大小,开关而存在。

我们可以在最开始请求Notifation的权限:

if (window.Notification && Notification.permission !== "granted") {
    Notification.requestPermission(function (status) {
        if (Notification.permission !== status) {
            Notification.permission = status;
        }
    });
}

然后创建一个通知实例

//第二个参数可选,其中icon为左侧图片的url, body为通知的内容
var options = {
    icon: "./pic.jpg",
    body: "hello, a demo of notifation"
}
//第一个参数为通知标题
var n = new Notification("The title!", options);

效果:
这里写图片描述

API参考:https://developer.mozilla.org/zh-CN/docs/Web/API/notification
兼容性:http://caniuse.com/#feat=notifications 不支持IE

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值