浏览器 Notification 通知消息

代码如下

<html>
<head>
	<meta charset="utf-8">
	<title>David Notification</title>
</head>
<body>
	<button id="notifyBtn">show Notification</button>
</body>
<script type="text/javascript">
		
	document.getElementById('notifyBtn').onclick = function(){
		console.log(Notification.permission);
		if(window.Notification && Notification.permission==='granted'){
			var info = {
				lang:'zh-CN', //语言
				body:'你有一条信息',//消息简介
				tag:'222on4'+new Date().getTime(), //相当于通知的id,保证信息的唯一性和禁止重复发送
				//icon:''//图片 
			};
			var notification = new Notification('Notification Center',info);

			if(!!notification){
				notification.onclick = function(evt){
					var e = evt || window.event;
					window.location.href = e.currentTarget.tag;
				};
				
				notification.onshow = function(evt){
					console.log("show",evt);
				};
				notification.onerror = function(evt){
					console.log("error",evt);
				};
				
				notification.onclose = function(evt){
					console.log("close",evt);
				};
			}
		}
    }
 
    window.onload = function(){
     	 if (window.Notification && Notification.permission!='granted'){
     	    Notification.requestPermission();//申请授权
     	 }
    }
</script>
</html>

注意事项

1、如果不显示,注意下windows通知设置,右下角消息->右击->专注助手->关闭

2、浏览器兼容情况如下:

查询地址:https://caniuse.com/?search=Notification

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值