2016-10-14 10 views
2

私は角ウェブ通知(https://github.com/sagiegurari/angular-web-notification)を使用しています。表示するたびにコピー貼り付けを避けるために工場を作りました。あなたは、私が(ショーに渡す見ることができるように私の工場は)3つの変数、そのうちの一つは、通知をクリックしたときにものを行うためには、onClickの機能のためのコールバックです。この角ウェブ通知onclickを閉じる

module.registerFactory('browserNotification', function($rootScope, webNotification) { 
    return { 
     show: function(title, body, callback) { 
      webNotification.showNotification(title, { 
       body: body, 
       icon: 'img/icon.png', 
       onClick: callback, 
       autoClose: 5000 //auto close the notification after 4 seconds (you can manually close it via hide function) 
      }, function onShow(error, hide) { 
       if (error) { 
        console.log('Unable to show notification: ' + error.message); 
       } else { 
        console.log('Notification Shown.'); 
        setTimeout(function hideNotification() { 
         console.log('Hiding notification....'); 
         hide(); //manually close the notification (you can skip this if you use the autoClose option) 
        }, 5000); 
       } 
      }); 
     } 
    } 
}) 

です。問題は、クリックした後にその通知を閉じることですが、hide()関数がコールバック関数が実行されるコンテキストに存在しないため、どのように把握できないかです。例えば、私のcontrllerで私はこれを持っています

browserNotification.show('Test title', 'Test body', function() { 
     hide(); 
     alert('Entro al callback!'); 
    }); 

そこにhide()は存在しませんでした。コールバック関数から通知を閉じるにはどうしたらいいですか?

答えて

0

これはトリックです!

module.registerFactory('browserNotification', function($timeout,webNotification) { 
     return { 
      show: function(title, body, callback) { 
       var snd = new Audio('audio/alert.mp3'); 
       snd.play(); 
       //the timeout is to sync the sound with the notification rendering on screen 
       $timeout(function() { 
        var hideNotification; 
        webNotification.showNotification(title, { 
         body: body, 
         icon: 'img/icon.png', 
         onClick: function onNotificationClicked() { 
          callback(); 
          if (hideNotification) { 
           hideNotification(); 
          } 
         }, 
         autoClose: 5000 //auto close the notification after 4 seconds (you can manually close it via hide function) 
        }, function onShow(error, hide) { 
         if (!error) { 
          hideNotification = hide; 
         } 
        }); 
       }, 150); 
      } 
     } 
    }); 
関連する問題