Change angular state after push reception


#1

Hello,

I have the following problem :
In my main controller, I have this function that is called when a push arrived on iOS device :

onNotificationAPN = function(event) {
if (event.alert) {
navigator.notification.alert(event.alert);
}
if (event.sound) {
var snd = new Media(event.sound);
snd.play();
}
if (event.badge) {
pushNotification.setApplicationIconBadgeNumber(successHandler, errorHandler, event.badge);
}
// do some work with event
$state.go(‘there’);
}

But it can’t work because $state is unknown.

Have you already tried to change the angular state after a push notification?

Thanks for your help


#2

It’s hard to tell from your sample, but I’m assuming that onNotificationAPN is a global function outside of Angular. Generally speaking, Angular doesn’t talk to the outside world. So, you have to resort to a few hacks to get that global function to talk to angular.

Here’s how I do it:

document.addEventListener('push-notification', function(event) {

    var body = document.getElementsByTagName("body")[0];
    var appController = angular.element(body).scope();
    appController.reportNotificationReceived(event);

});

Here’s an explanation of how this works : http://calendee.com/2014/05/12/custom-urls-in-ionic-cordova-apps/

Also, here’s another post about similar subject : How to integrate Cordova/Phonegap plugins within Angular/Ionic the right way?


#3

The problem was this object beacause it wad undefined

event.badge

The change of state is now OK