首先,这与几个问题非常相似,但没有一个答案对我有用。
我有一个Cordova / AngularJS / Ionic应用程序,每10分钟轮询一个远程服务器并下拉一些JSON - 这很好用。我也有PhoneGap Build插件的推送通知,再次正常工作。我想要做的就是连接这两个,这样当一个通知进来时它会激活我的轮询器,从而获得轮询时间之间的最新内容。
现有功能代码:
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
} // END pollerFunct()
// run on app startup, then every pollTimeout duration
// delay by 10sec so that checkConnectionIsOn() has time to settle on browser platform - seems not needed in actual devices but no harm to delay there too
setTimeout(function(){ pollerFunct(); }, 10000);
//pollerFunct(); // run on app startup, then every pollTimeout duration
$interval(pollerFunct, pollTimeout);
}) // END factory Poller
在Angular之外推送通知处理
// handle GCM notifications for Android
function AndroidOnNotification(e) {
// working: call angular service from outside angular: http://stackoverflow.com/questions/15527832/how-can-i-test-an-an-angularjs-service-from-the-console
var $http = angular.element(document.body).injector().get('$http');
var $state = angular.element(document.body).injector().get('$state');
// not working : http://stackoverflow.com/questions/26161638/how-to-call-an-angularjs-factory-function-from-outside
angular.element(document.body).injector().get('Poller').pollerFunct();
}
我想从AndroidOnNotification(e)调用pollerFunct()但是得到" processMessage失败:错误:TypeError:undefined不是函数"和类似的错误。
答案 0 :(得分:0)
@Petr的评论没有任何回复导致我检查我忘记的代码的其他部分,这有用:
.factory('Poller', function() {
var pollerFunct = function() {
// fetch and process some JSON from remote server
}
// return something
return {
poll: function() {
pollerFunct();
return;
}
}
})
被叫:
angular.element(document.body).injector().get('Poller').poll();