如何在ngCordova push插件中处理onNotification事件

时间:2014-10-01 04:02:57

标签: ionic-framework cordova-plugins

作为代码模板,像这样做,

.controller('PushNotificationsCtrl', function ($scope, $cordovaPush) {
var androidConfig = {
"senderID":"372433177444",
"ecb":"onNotification"
};

$cordovaPush.register(androidConfig).then(function(result) {
  // Success!
  $scope.pushSuccess = result
}, function(err) {
  $scope.pushSuccess = err;
});

我设法成功从GCM获得RegID。但是,如何从onNotification管理androidConfig

2 个答案:

答案 0 :(得分:5)

我找到了解决方案。

而不是这样做:

var androidConfig = {
"senderID":"372433177444",
"ecb":"onNotification"
};

我喜欢这个:

var androidConfig = {
"senderID":"372433177444",
"ecb":"window.onNotification"
};

然后

window.onNotification = function(e) {
  switch( e.event )
  {
      case 'registered':
          if ( e.regid.length > 0 )
          {
              console.log("Your regID is : " + e.regid);
          }
          break;

      case 'message':
          // this is the actual push notification. its format depends on the data model     from the push server
          console.log('message = '+e.message);
          angular.element(document.querySelector('#yata')).html(e.message);
          break;

      case 'error':
          console.log('GCM error = '+e.msg);
          break;

      default:
          console.log('An unknown GCM event has occurred');
          break;
  }
};

现在按预期工作:)

答案 1 :(得分:0)

将以下代码更改为 $this->output->set_header('Last-Modified:' . gmdate('D, d M Y H:i:s') . 'GMT'); $this->output->set_header('Cache-Control: no-cache, no-cache, must-revalidate'); $this->output->set_header('Cache-Control: post-check=0, pre-check=0', false); $this->output->set_header('Pragma: no-cache');

ng-cordova.js

然后按如下所示设置选项配置对象:

//config.ecb = 'angular.element(' + injector + ').injector().get(\'$cordovaPush\').onNotification';
config.ecb = "angular.element(" + injector + ").injector().get('$cordovaPush').onNotification";

打开应用程序时,它会在通知抽屉中显示通知。