2

I'm using angular-web-notification (https://github.com/sagiegurari/angular-web-notification) and i've built a factory in order to avoid copy-pasting every time I want to display it. My factory is this

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

As you can see I pass to the show() 3 variables, one of them is a callback for the onClick function, in order to do stuff when the notification is clicked. The thing is that i want to close that notification once its clicked, but i can't figure out how, because the hide() functions doesn´t exist in the context where the callback function is executed. For example, in my contrller I have this

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

There, hide() didn't exist. So, how can I close the notification from my callback function?

MarBVI
  • 811
  • 2
  • 12
  • 34

1 Answers1

0

This makes the trick!

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);
            }
        }
    });
MarBVI
  • 811
  • 2
  • 12
  • 34