ionic framework action back button not working

I'm having trouble with the action back from one of my views of my application. What I need is to send a warning message to the user before it gets out of sight in this. For example you sure leave this page?.

I found two ways to control this action but I can not make it run INSIDE AND BEFORE you leave the view.

1 :

$scope.$on('$ionicView.beforeLeave', function(event){
alert("Before Leaving");
$ionicPopup.show({
   title: 'View',
   subTitle: '',
   content: '¿Esta seguro de no publicar este view?',
   buttons: [
     { text: 'No',
      onTap: function(e){

      }
     },
     {
       text: 'Si',
       type: 'button-positive',
       onTap: function(e) {
            $ionicHistory.goBack();
       }
     },
   ]
  })
});

2:

$scope.$on("$ionicView.leave", function(event){
event.preventDefault();
event.stopPropagation();
$ionicPopup.show({
   title: 'View',
   subTitle: '',
   content: '¿Esta seguro de no publicar este view?',
   buttons: [
     { text: 'No',
      onTap: function(e){

      }
     },
     {
       text: 'Si',
       type: 'button-positive',
       onTap: function(e) {
            $ionicHistory.goBack();
       }
     },
   ]
  })
 })

Both functions are running, but run after leaving the current view in which the user and not before as I need. In addition, add the event.stopPropagation and event.preventDefault event and do not function.