2

我一直在关注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模拟器上获取(并注册)推送通知?

  1. 为什么我不能获得调用我的回调的$ 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"); 
    } 
    

回答

2

我有同样的问题,并已在ngCordova GitHub的报道,但没有答复呢。

我设法解决它。我知道这是不是一个很好的解决方案,如果您使用的角度,但它是唯一的方法我能够赶上寄存器Id。

  1. 里面的配置对象必须指定'ecb'

    var androidConfig = { 
        "senderID": "388573974286", 
        "ecb": "function_to_be_called" 
    }; 
    
  2. 控制器外放功能:



     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; 
     } 
    }; 

+0

这样做!问题是......为什么它不在控制器内工作... – Txugo 2015-02-23 22:30:59

+0

我有一个类似的问题,它可以很好地处理控制器中的所有内容,但是当我将实现移动到一个.factory时它会停止工作。您的解决方案将“window.function_to_be_called”函数放入工厂。我不明白为什么这会工作,但$ scope。$ on('$ cordovaPush:notificationReceived',函数(事件,通知)'不。 – 2015-06-02 20:03:31

4

修复的方法是简单了很多似乎比。 我改变来自:

$scope.$on('$cordovaPush:notificationReceived', function (event, notification) { 

到:

$scope.$on('pushNotificationReceived', function (event, notification) { 

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广播”。 Not'notificationReceived'为documented

+0

也在我的情况下遵循http://devgirl.org/2014// 12/16/push-notifications-sample-app-with-ionic-and-ngcordova/tutorial我需要评论这一行$ scope。$ apply(function(){into handleAndroid function – gastonnina 2015-05-08 05:05:23

+0

这很烦人,他们离开这部分在文档中,仍然没有解决这个简单的线路,激活推送通知的过程本身令人沮丧,没有它...谢谢你的答案!你保存了一天。:) – Shay 2015-05-18 13:12:19

0

即使您更新事件名称,如您所说,我也有另一个问题,它说:processmessage failed: message: jjavascript:angular.element(document.queryselector('[ng-app]')).injector().get('$cordovapush').onnotification({"event":"registered" ...

它发生时的角度没有找到“NG-应用程序”,并返回“未定义” angular.element(document.querySelector('[ng-app]'))

因此,要解决这个问题,你可以手动设置你的“ecb”是这样的:

angular.element(document.body).injector().get('$cordovaPush').onNotification 

由于这个ionic forum entry

+0

你有最新的cordovaPush的版本?[2.4.0] – Txugo 2015-02-24 23:39:53

+0

是的,确切的说 – jamlet 2015-02-25 11:23:55