ngCordova/Ionic 推送通知,仅收到 "OK"

Push notifications in ngCordova/Ionic, getting just "OK"

我第一次尝试使用 this example code for Android 创建混合应用程序。我正在测试是否可以让通知正常工作,但我遇到了两个错误:

  1. 你应该得到的 result 对象不包含任何东西,只是 OK

  2. 我得到$rootScope is not defined,我应该在哪里定义它?

我的密码是

var app = angular.module('starter', ['ionic', 'ngCordova']);

app.run(function($cordovaPush) {

  var androidConfig = {
    "senderID": "84xxxxxxxx",
  };

  document.addEventListener("deviceready", function(){
    $cordovaPush.register(androidConfig).then(function(result) {
      // Success
      console.log("OK, result is " + result);
    }, function(err) {
      // Error
      console.log("NOT OK");
    })

    $rootScope.$on('$cordovaPush:notificationReceived', function(event, 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;
      }
    });


    // WARNING: dangerous to unregister (results in loss of tokenID)
    $cordovaPush.unregister(options).then(function(result) {
      // Success!
    }, function(err) {
      // Error
    })

  }, false);
});

像这样在 .运行() 中包含 $rootScope

app.run(function($cordovaPush,$rootScope) {

resultregister响应中是没有任何用处的you.In注册成功的情况下你会收到一个事件registered,你需要的是注册id,您将在此处收到 notification.regid.

 case 'registered':
          if (notification.regid.length > 0 ) {
            alert('registration ID = ' + notification.regid);
          }
          break;