我一直在关注this sample以获取android pushNotifications(使用GCM)在Android模拟器上工作。
在$cordovaPush.register(config)
之后我得到了回复。但它永远不会运行我的回调[$scope.$on('$cordovaPush:notificationReceived'
]。
因此,我从未获得我的注册ID。
我创建了一个google API项目。我在调用$cordovaPush.register(config)
时在config.senderID中使用该项目ID。
我还在我的模拟器中注册了一个gmail帐户。
我想我有2个问题。
1.是否可以在Android模拟器上获取(并注册)推送通知?
为什么我没有收到调用我的回调的$ cordovaPush:notificationReceived事件?
app.controller('AppCtrl',函数($ scope,$ cordovaPush,$ cordovaDialogs,$ cordovaMedia,$ cordovaToast,ionPlatform,$ http){ $ scope.notifications = [];
// call to register automatically upon device ready
ionPlatform.ready.then(function (device) {
$scope.register();
});
// Register
$scope.register = function () {
var config = null;
if (ionic.Platform.isAndroid()) {
config = {
"senderID": "12834957xxxx"
};
}
$cordovaPush.register(config).then(function (result) {
console.log("Register success " + result);
$cordovaToast.showShortCenter('Registered for push notifications');
$scope.registerDisabled=true;
}, function (err) {
console.log("Register error " + err)
});
}
$scope.$on('$cordovaPush:notificationReceived', function (event, notification) {
console.log(JSON.stringify([notification]));
if (ionic.Platform.isAndroid()) {
handleAndroid(notification);
}
});
// Android Notification Received Handler
function handleAndroid(notification) {
// ** NOTE: ** You could add code for when app is in foreground or not, or coming from coldstart here too
// via the console fields as shown.
console.log("In foreground " + notification.foreground + " Coldstart " + notification.coldstart);
if (notification.event == "registered") {
$scope.regId = notification.regid;
storeDeviceToken("android");
}
else if (notification.event == "message") {
$cordovaDialogs.alert(notification.message, "Push Notification Received");
$scope.$apply(function () {
$scope.notifications.push(JSON.stringify(notification.message));
})
}
else if (notification.event == "error")
$cordovaDialogs.alert(notification.msg, "Push notification error event");
else $cordovaDialogs.alert(notification.event, "Push notification handler - Unprocessed Event");
}
答案 0 :(得分:4)
修复比看起来简单得多。 我改变了:
$scope.$on('$cordovaPush:notificationReceived', function (event, notification) {
为:
$scope.$on('pushNotificationReceived', function (event, notification) {
while debugging我在ng-cordova.js上注意到了这一点:
angular.module('ngCordova.plugins.push', [])
.factory('$cordovaPush', ['$q', '$window', '$rootScope', function ($q, $window, $rootScope) {
return {
onNotification: function (notification) {
$rootScope.$apply(function () {
$rootScope.$broadcast('pushNotificationReceived', notification);
});
},
这意味着它正在为'pushNotificationReceived'进行广播。没有' notificationReceived'为documented。
答案 1 :(得分:2)
我有同样的问题。有人在ngCordova github上报道过,但还没有回复。
我设法解决了这个问题。我知道如果你使用角度,这不是一个好的解决方案,但这是我能够捕获寄存器Id的唯一方法。
在配置对象中,您必须指定'ecb'
:
var androidConfig = {
"senderID": "388573974286",
"ecb": "function_to_be_called"
};
在控制器外面放置功能:
window.function_to_be_called = function (notification) {
switch(notification.event) {
case 'registered':
if (notification.regid.length > 0 ) {
alert('registration ID = ' + notification.regid);
}
break;
case 'message':
// this is the actual push notification. its format depends on the data model from the push server
alert('message = ' + notification.message + ' msgCount = ' + notification.msgcnt);
break;
case 'error':
alert('GCM error = ' + notification.msg);
break;
default:
alert('An unknown GCM event has occurred');
break;
}
};
答案 2 :(得分:0)
即使您按照说明更新了活动名称,我也会遇到另一个问题:processmessage failed: message: jjavascript:angular.element(document.queryselector('[ng-app]')).injector().get('$cordovapush').onnotification({"event":"registered"
...
当角度没有找到' ng-app'它返回' undefined' angular.element(document.querySelector('[ng-app]'))
因此,要解决此问题,您可以手动设置' ecb
'像这样:
angular.element(document.body).injector().get('$cordovaPush').onNotification
答案 3 :(得分:0)
请在ng-cordova-master \ dist \ ng-cordova.js中的http://ngcordova.com/docs/install/更新lib / ng-cordova.js中的ng-cordova.js