HTML5 通知使用情况

HTML5 Notifications Usage

我有一个 angular 实时消息传递应用程序,我想使用 HTML5 通知。我在控制器中尝试了以下操作:

$rootScope.$on("message_recieved", function(event, data) {
  new Notification(data.sender_name, {
    body: data.body, icon: data.avatar, dir:'auto'
  });
}

$rootScope.$on("server_offline", function(event, data) {
  new Notification("Offline", {
    body: "You are offline. Refresh your page now.", dir:'auto'
  });
}

通知没有显示。我做错了什么吗?

很可能您不被允许显示通知。您可以申请权限:

$rootScope.$on( 'message_received', function(event, data){
     Notification.requestPermission(function (permission){
          if (permission === "granted"){
               new Notification(data.sender_name, {
                    body: data.body, icon: data.avatar, dir:'auto'
               })
          }
      })
 })

src - https://developer.mozilla.org/en-US/docs/Web/API/notification