简体   繁体   English

HTML5通知用法

[英]HTML5 Notifications Usage

I have an angular real-time messaging application and I wanted to use HTML5 notifications. 我有一个角度实时消息传递应用程序,我想使用HTML5通知。 I tried the following inside my controller: 我在控制器中尝试了以下操作:

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

and

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

The notifications are not showing. 通知未显示。 Am I doing anything wrong here? 我在这里做错什么吗?

Chances are you are not permitted to display Notifications. 您可能无法显示通知。 You can request permission: 您可以请求权限:

$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 src- https://developer.mozilla.org/zh-CN/docs/Web/API/notification

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM