HTML5通知用法

时间:2015-12-15 16:44:43

标签: html angularjs html5 notifications html5-notifications

我有一个角度实时消息传递应用程序,我想使用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'
  });
}

通知未显示。我在这里做错了吗?

1 个答案:

答案 0 :(得分:1)

您可能无法显示通知。您可以申请许可:

$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