当前位置 : 主页 > 手机开发 > cordova >

ionic-framework – 如何在ngCordova push插件中处理onNotification事件

来源:互联网 收集:自由互联 发布时间:2021-06-10
作为代码模板这样做, .controller('PushNotificationsCtrl', function ($scope, $cordovaPush) {var androidConfig = {"senderID":"372433177444","ecb":"onNotification"};$cordovaPush.register(androidConfig).then(function(result) { // Succes
作为代码模板这样做,

.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.但是,我如何从androidConfig管理onNotification?

我找到了解决方案.

而不是这样做:

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

现在按预期工作了:)

网友评论