我一直在关注this sample以获取android pushNotifications(与GCM)在Android模拟器上工作。 $cordovaPush.register(config)
之后我收到了答复。但它永远不会运行我的回调[$scope.$on('$cordovaPush:notificationReceived'
]。 因此我永远不会得到我的注册ID。Android PushNotifications与仿真器上的PushPlugin
我创建了一个google API项目。在调用$cordovaPush.register(config)
时,我在config.senderID中使用该项目Id。
我也注册了一个Gmail帐户到我的模拟器。
我想我有2个问题。
1.是否可以在android模拟器上获取(并注册)推送通知?
为什么我不能获得调用我的回调的$ cordovaPush:notificationReceived事件?
app.controller( 'AppCtrl',函数($范围,$ 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"); }
这样做!问题是......为什么它不在控制器内工作... – Txugo 2015-02-23 22:30:59
我有一个类似的问题,它可以很好地处理控制器中的所有内容,但是当我将实现移动到一个.factory时它会停止工作。您的解决方案将“window.function_to_be_called”函数放入工厂。我不明白为什么这会工作,但$ scope。$ on('$ cordovaPush:notificationReceived',函数(事件,通知)'不。 – 2015-06-02 20:03:31