离子应用程序 - CordovaPush 不工作

Ionic App - CordovaPush not working

我添加了 phone 间隙推送插件和 ngcordova。 ngcordova.js 已在 cordova.js 之前导入 我在浏览器中收到 $window.plugin not defined 错误,我认为这是预料之中的。但是我在应用程序中收到 cannot read property register of undefined 错误,这意味着 $cordovaPush 未定义。

我看过 this 教程并尝试实现相同的

 angular
    .module('init')
    .run(function($ionicPlatform, $rootScope, $state, $cordovaPush, $cordovaToast){
      $ionicPlatform.ready(function() {
        // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
        // for form inputs)
        if(window.cordova && window.cordova.plugins.Keyboard) {
          cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
        }

        if(window.StatusBar) {
          window.StatusBar.styleDefault();
        }
        registerForPush();

        function registerForPush(){
          try {

          var config = {};
          //register for push notifications
          if (ionic.Platform.isAndroid()) {
            config = {
              "senderID": "1232"
            };
          }else if (ionic.Platform.isIOS()) {
            config = {
              "badge": "true",
              "sound": "true",
              "alert": "true"
            }
          }

            $cordovaPush.register(config).then(function (result) {
            alert("Register success " + result);
            $cordovaToast.showShortCenter('Registered for push notifications');
            // ** NOTE: Android regid result comes back in the pushNotificationReceived, only iOS returned here
            if (ionic.Platform.isIOS()) {
              //register
            }
          }, function (err) {
            console.log("Register error " + err)
          });

          }
          catch(err) {
            alert(err.message);
          }

        }


        // Notification Received
        $rootScope.$on('$cordovaPush:notificationReceived', function (event, notification) {
          alert(JSON.stringify([notification]));
          if (ionic.Platform.isAndroid()) {
            if (notification.event == "registered") {
              alert("android notification received token - " + notification.regid);
            }

          }
          else if (ionic.Platform.isIOS()) {
           alert("ios notification received");
          }
        });



        document.addEventListener('resume', function () {
          $rootScope.$broadcast('resume');
        });
        document.addEventListener('pause', function () {
          $rootScope.$broadcast('pause');
        });
        document.addEventListener('offline', function () {
          $rootScope.$broadcast('offline');
        });
        document.addEventListener('online', function () {
          $rootScope.$broadcast('online');
        });
        window.addEventListener('batterystatus', function (status) {
          $rootScope.$broadcast('batterystatus', status);
        });

      });
    });

我开发了一个推送应用程序,得到了同样的错误,但不是用这个插件: Plugin-push

删除 ng-cordova 插件并尝试使用该插件,它将起作用。

按照本教程获取更多帮助:doc ionic

我已经通过 Phonegap Push Plugin

成功实现了这个功能

发起推送

var push = PushNotification.init({
    android: {
        senderID: "12345"
    },        
});

push.on('registration', function(data) {
    // data.registrationId this device token is used to send push notifications
});

我已经使用 http://www.plugreg.com/plugin/phonegap-build/PushPlugin 开发推送通知并且效果很好。

首先,卸载任何其他推送通知插件,然后尝试这个 one.It 工作。