web notification api

Web Notifications API 使页面可以发出通知,通知将被显示在页面之外的系统层面上(通常使用操作系统的标准通知机制,但是在不同的平台和浏览器上的表现会有差异)

要显示一条通知,你需要先请求适当的权限。Web Notifications API 请求 权限有两种方式

1、Notification.requestPermission(callback)      

2、Notification.requestPermission().then(function(status){  ...  })

Notification.requestPermission().then(function(status) {
    console.log(status)            //status有三种状态  default默认(会询问用户是否开启通知的权限granted已经允许开启通知    denied用户已经明确的拒绝了显示通知的权限。
  if(status == 'granted'){ 
     
new Notification(title, { //new Notification是发送通知 title是标题 body是通知的内容 icon发送人的图标
        body:
'can i addfriend you',
        icon:
'http://combustion-engines.oss-cn-shanghai.aliyuncs.com/CombustionEnginesTupian/2017/dfbb2d68-97e4-4b9f-8d5f-82a636e3edcc.png'
}) } });

 详情可以去这里查看MDN web notification api

原文地址:https://www.cnblogs.com/guojikun/p/7274678.html