2

angular-web-notification ( https://github.com/sagiegurari/angular-web-notification ) を使用しており、表示するたびにコピーして貼り付けるのを避けるためにファクトリを構築しました。私の工場はこれです

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);
                }
            });
        }
    }
})

ご覧のとおり、show() 3 つの変数に渡します。そのうちの 1 つは onClick 関数のコールバックで、通知がクリックされたときに処理を実行します。クリックしたらその通知を閉じたいのですが、コールバック関数が実行されるコンテキストに hide() 関数が存在しないため、その方法がわかりません。たとえば、私のコントローラにはこれがあります

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

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

4

1 に答える 1

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);
            }
        }
    });
于 2016-10-14T21:03:39.203 に答える