the callback function is a global function like this:
var onNotificationAPN = function(event){
//show a modal here...
}
My question is, if I want to show a modal in this callback, how can I achieve this?
Modals are defined in controller, how can I access the modal and show it in a global function?
You should be able to move this to a controller and have no issues.
.controller('MyCtrl', function ($scope, $ionicPlatform) {
$ionicPlatform.ready(function () {
alert('Ionic And Cordova ready');
});
});
This way you can set up everything and make sure it all happens within the ionicPlatform.ready.
Tested it out on an actual device and got things to work.
I want to show a view when user clicks the push notification, that means I need to do something in the onNotificationAPN function.
In the github README doc, onNotificationAPN is a global function, so it’s OK.
However, in my case, I want to register a function in my controller, so I can call $location or $scope to change the view.
Seems like the same questions are popping up… Instead of trying to call back directly to my Angular code, I used a couple of lines of code in the following manner:
//This works for me because in my code I have a factory called
// PushProcessingService with method registerID
var elem = angular.element(document.querySelector('[ng-app]'));
var injector = elem.injector();
var myService = injector.get('PushProcessingService');
myService.registerID(e.regid);
Or if all you want to do is redirect somewhere, you can just use window.location. More info here.
Thanks for your reply.
Even though you use PushWoosh, you still need to show something when user clicks the push notification, right?
PushWoosh callback is like this:
document.addEventListener('push-notification', function(event) {
var notification = event.notification;
navigator.notification.alert(notification.aps.alert);
pushNotification.setApplicationIconBadgeNumber(0);
});
This is almost the same with my case.
If I want to show a modal here, what can I do?
Have you tried removing $scope from the onNotificationAPN It’s a shot in the dark because PushWoosh calls its own notification window so I’ve never had to deal with it.
Complete noob to push notifications. Are you talking about dealing with these push notifications happen while the app is open or while the app is closed. I’m assuming while the app is open.
My suggestion would be to have the push notification update a service. Then, in some controller, do a watch on the value in the service. If the service value “hasNotification” changes, then the controller will open the modal.
Thanks, Calendee.
Indeed, what I need is how to communicate with the angular controller module from a native global js function.
Your advice is quite smart to use the service as the media of communication between native js and angular, I think it can solve my problem.
However, I found out another simple way before your advice:
At first, I try to show a modal on notification, and failed.
Then, I decided to redirect to a view like this:
window.location.href = "#/menu/promotions"
And it works, without talking to angular directly.
The solution is so simple, I didn’t think of this because angular still works like a magic to me.
Sorry to bother everyone for this!