Although this question is similar to others posted here, none of the solutions provided have worked for me.
My Cordova/AngularJS/Ionic app currently polls a remote server every 10 minutes to retrieve JSON data, and push notifications are working correctly using the PhoneGap Build plugin. What I am trying to achieve is to integrate the push notifications with the polling mechanism, so that when a notification is received, it triggers the poller to fetch the latest content in between the regular poll intervals.
Here is the existing code that is functioning:
var schoolApp = angular.module('schoolApp', ['ionic', 'schoolApp.controllers', 'schoolApp.services'])
schoolApp.run(function(Poller) {});
.factory('Poller', function($http, $interval,updateContentLocalStorage) {
var pollerFunct = function() {
// fetch and process some JSON from remote server
}
// run on app startup, then every pollTimeout duration
setTimeout(function(){ pollerFunct(); }, 10000);
$interval(pollerFunct, pollTimeout);
}) // END factory Poller
Push notification processing, outside of Angular
// handle GCM notifications for Android
function AndroidOnNotification(e) {
var $http = angular.element(document.body).injector().get('$http');
var $state = angular.element(document.body).injector().get('$state');
// Issue: unable to call pollerFunct() from AndroidOnNotification(e)
angular.element(document.body).injector().get('Poller').pollerFunct();
}
My goal is to successfully call the pollerFunct() function from within the AndroidOnNotification(e) function without encountering errors such as "processMessage failed: Error: TypeError: undefined is not a function."