Android 在ionic应用程序中实现推送通知

Android 在ionic应用程序中实现推送通知,android,push-notification,ionic-framework,ionic,Android,Push Notification,Ionic Framework,Ionic,我正在尝试在安卓版的爱奥尼亚应用程序中实现推送通知。 我已经按照指南一步一步地做了。 当我在android手机上运行我的应用程序时,注册用户将列在apps.ionic.io中。因此,用户注册工作正常。 但设备注册不起作用。 它发出错误无法读取未定义的属性“pushNotification” 这是我在app.js顶部的代码 angular.module('starter', ['ionic','ngCordova', 'ionic.service.core', 'ionic.ser

我正在尝试在安卓版的爱奥尼亚应用程序中实现推送通知。 我已经按照指南一步一步地做了。 当我在android手机上运行我的应用程序时,注册用户将列在apps.ionic.io中。因此,用户注册工作正常。 但设备注册不起作用。 它发出错误无法读取未定义的属性“pushNotification”

这是我在app.js顶部的代码

angular.module('starter', ['ionic','ngCordova',
    'ionic.service.core',
    'ionic.service.push',
    'starter.controllers',
    'starter.services'])

    .config(['$ionicAppProvider', function($ionicAppProvider) {
        // Identify app
        $ionicAppProvider.identify({
            // The App ID (from apps.ionic.io) for the server
            app_id: '',
            // The public API key all services will use for this app
            api_key: '',
            // Set the app to use development pushes
           // dev_push: true
            gcm_id: ''
        });
    }])
这是我的控制器中的代码

 .controller('DashboardCtrl', function($scope,$localstorage, WildfireService, CommonUtilityService,PushNotificationService,$ionicPopup, $ionicLoading) {      
      PushNotificationService.identifyUser();
      PushNotificationService.pushRegister();

    })
这是我的services.js

 .service('PushNotificationService', function($q, $ionicUser, $ionicPush) {
        var PushNotificationService = this;
        PushNotificationService.identifyUser  = function(){
            var user = $ionicUser.get();
            if(!user.user_id) {
                // Set your user_id here, or generate a random one.
                user.user_id = $ionicUser.generateGUID();
            };

            // Add some metadata to your user object.
            angular.extend(user, {
                name: 'Technews',
                bio: 'Hardcoded for now'
            });

            // Identify your user with the Ionic User Service
            $ionicUser.identify(user).then(function(){
                //alert('Identified user ' + user.name + '\n ID ' + user.user_id);
                return true;
            });
        },

        PushNotificationService.pushRegister = function(){
            // Register with the Ionic Push service.  All parameters are optional.
            $ionicPush.register({
                canShowAlert: true, //Can pushes show an alert on your screen?
                canSetBadge: true, //Can pushes update app icon badges?
                canPlaySound: true, //Can notifications play a sound?
                canRunActionsOnWake: true, //Can run actions outside the app,
                onNotification: function(notification) {
                    // Handle new push notifications here
                    // console.log(notification);
                    alert(notification);
                    return true;
                }
            });
        }
    })
 .service('PushNotificationService', function($q, $ionicUser, $ionicPush) {
            var PushNotificationService = this;
            PushNotificationService.identifyUser  = function(){
                var user = $ionicUser.get();
                if(!user.user_id) {
                    // Set your user_id here, or generate a random one.
                    user.user_id = $ionicUser.generateGUID();
                };

                // Add some metadata to your user object.
                angular.extend(user, {
                    name: 'Technews',
                    bio: 'Hardcoded for now'
                });

                // Identify your user with the Ionic User Service
                $ionicUser.identify(user).then(function(){
                    //alert('Identified user ' + user.name + '\n ID ' + user.user_id);
PushNotificationService.pushRegister();
                    return true;
                });
            },

            PushNotificationService.pushRegister = function(){
                // Register with the Ionic Push service.  All parameters are optional.
                $ionicPush.register({
                    canShowAlert: true, //Can pushes show an alert on your screen?
                    canSetBadge: true, //Can pushes update app icon badges?
                    canPlaySound: true, //Can notifications play a sound?
                    canRunActionsOnWake: true, //Can run actions outside the app,
                    onNotification: function(notification) {
                        // Handle new push notifications here
                        // console.log(notification);
                        alert(notification);
                        return true;
                    }
                });
            }
        })
有人能告诉我哪里出了错,我遗漏了什么吗

我已经在index.html中添加了这些

<script src="lib/ionic/js/ionic.bundle.js"></script>
    <script src="lib/ngCordova/dist/ng-cordova.js"></script>
    <script src="lib/ionic-service-core/ionic-core.js"></script>
    <script src="lib/ionic-service-push/ionic-push.js"></script>

我不知道这个新的离子推送通知。我通过ngCordova的推送插件成功地在我的Ionic应用程序中实现了推送通知,该插件适用于Android和iOS(),只需遵循其中的示例即可。关键是将代码放在app.js的.run模块中

代码如下:

angular.module('notPush', ['ionic','notPush.controllers','notPush.factorys','ngCordova','ionic.service.core'])

.run(function($ionicPlatform, $rootScope, $http, $cordovaPush) {
  $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) {
      StatusBar.styleDefault();
    }

    // Manejador de notificaciones push para Android
    if (ionic.Platform.isAndroid()){

      var androidConfig = {
        "senderID": "94XXXXXXXXXX", // ID HERE
        "ecb": "window.casosPush"
      };

      try{
        var pushNotification = window.plugins.pushNotification;
      } catch (ex){

      }

      // Llamada en caso de exito
      var successfn = function(result){
        alert("Success: " + result);
      };

      // Llamada en caso de error
      var errorfn   = function(result){
        window.alert("Error: " + result);
      };

      // Llamada de casos de notificacion push
      window.casosPush = function(notification){
        switch (notification.event){
          case 'registered':
            if (notification.regid.length > 0){
              alert('registration ID = ' + notification.regid);
            }
            break;

          case 'message':
            alert(JSON.stringify([notification]));
            break;

          case 'error':
            alert('GCM error = ' + notification.msg);
            break;

          default:
            alert('An unknown GCM event has occurred');
            break;
        }
      };
      try{

        // Llamada de registro con la plataforma GCM 
        pushNotification.register(successfn,errorfn,androidConfig);
      } catch(notification){

      }
    }
})
只需记住使用安装插件即可

cordova plugin add https://github.com/phonegap-build/PushPlugin.git

最后,推送通知对我有效。我将pushRegister的函数调用从controller移动到identifyUser函数。这是我的新工作代码。 控制器代码

.controller('DashboardCtrl', function($scope,$localstorage, WildfireService, CommonUtilityService,PushNotificationService,$ionicPopup, $ionicLoading) {      
      PushNotificationService.identifyUser();


    })
这里是newservices.js

 .service('PushNotificationService', function($q, $ionicUser, $ionicPush) {
        var PushNotificationService = this;
        PushNotificationService.identifyUser  = function(){
            var user = $ionicUser.get();
            if(!user.user_id) {
                // Set your user_id here, or generate a random one.
                user.user_id = $ionicUser.generateGUID();
            };

            // Add some metadata to your user object.
            angular.extend(user, {
                name: 'Technews',
                bio: 'Hardcoded for now'
            });

            // Identify your user with the Ionic User Service
            $ionicUser.identify(user).then(function(){
                //alert('Identified user ' + user.name + '\n ID ' + user.user_id);
                return true;
            });
        },

        PushNotificationService.pushRegister = function(){
            // Register with the Ionic Push service.  All parameters are optional.
            $ionicPush.register({
                canShowAlert: true, //Can pushes show an alert on your screen?
                canSetBadge: true, //Can pushes update app icon badges?
                canPlaySound: true, //Can notifications play a sound?
                canRunActionsOnWake: true, //Can run actions outside the app,
                onNotification: function(notification) {
                    // Handle new push notifications here
                    // console.log(notification);
                    alert(notification);
                    return true;
                }
            });
        }
    })
 .service('PushNotificationService', function($q, $ionicUser, $ionicPush) {
            var PushNotificationService = this;
            PushNotificationService.identifyUser  = function(){
                var user = $ionicUser.get();
                if(!user.user_id) {
                    // Set your user_id here, or generate a random one.
                    user.user_id = $ionicUser.generateGUID();
                };

                // Add some metadata to your user object.
                angular.extend(user, {
                    name: 'Technews',
                    bio: 'Hardcoded for now'
                });

                // Identify your user with the Ionic User Service
                $ionicUser.identify(user).then(function(){
                    //alert('Identified user ' + user.name + '\n ID ' + user.user_id);
PushNotificationService.pushRegister();
                    return true;
                });
            },

            PushNotificationService.pushRegister = function(){
                // Register with the Ionic Push service.  All parameters are optional.
                $ionicPush.register({
                    canShowAlert: true, //Can pushes show an alert on your screen?
                    canSetBadge: true, //Can pushes update app icon badges?
                    canPlaySound: true, //Can notifications play a sound?
                    canRunActionsOnWake: true, //Can run actions outside the app,
                    onNotification: function(notification) {
                        // Handle new push notifications here
                        // console.log(notification);
                        alert(notification);
                        return true;
                    }
                });
            }
        })