【问题标题】:Ionic App - CordovaPush not working离子应用程序 - CordovaPush 不工作
【发布时间】:2016-02-17 13:37:50
【问题描述】:

我添加了电话间隙推送插件和 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);
        });

      });
    });

【问题讨论】:

    标签: javascript angularjs cordova push-notification ionic-framework


    【解决方案1】:

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

    删除 ng-cordova 插件并尝试使用那个插件,它会工作。

    关注本教程以获得更多帮助:doc ionic

    【讨论】:

    • 我很困惑。不使用插件推送是什么意思?你能放一些示例代码吗
    • 我正在尝试使用原生通知而不是离子推送!
    【解决方案2】:

    我已经设法通过使用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
    });
    

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-05
        • 2018-09-22
        • 1970-01-01
        • 1970-01-01
        • 2018-03-01
        相关资源
        最近更新 更多