我一直在关注这个示例来让 android pushNotifications(使用 GCM)在 android 模拟器上工作。在$cordovaPush.register(config)
我得到 Ok 作为回应之后。但它从不运行我的回调 [ $scope.$on('$cordovaPush:notificationReceived'
]。结果我从来没有得到我的注册ID。
我创建了一个谷歌 API 项目。我在调用 config.senderID 时使用了该项目 ID $cordovaPush.register(config)
。
我还在模拟器中注册了一个 gmail 帐户。
我想我有2个问题。
1.是否可以在安卓模拟器上获取(和注册)推送通知?
为什么我没有收到调用我的回调的 $cordovaPush:notificationReceived 事件?
app.controller('AppCtrl', function($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"); }