1

Я добавил плагин для подключения к телефону и ngcordova. ngcordova.js был импортирован до cordova.js Я получаю ошибку $window.plugin not defined в браузере, который, как я полагаю, ожидается. Но я получаю ошибку cannot read property register of undefined в приложении, что означает, что $cordovaPush не определено.Ионное приложение - 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); 
     }); 

     }); 
    }); 

ответ

0

мне удалось получить эту работу, используя 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 
}); 
0

Я разработал нажимное приложение, и получил ту же ошибку, но не с помощью этого плагина: Plugin-push

Снимите нг -кордова плагин и попробуй с этим, он будет работать.

Следуйте этому учебник для получения дополнительной помощи: doc ionic

+0

Я смущен. что вы подразумеваете, не используя Plugin-push? Можете ли вы добавить пример кода – Coder

+0

Я пытаюсь использовать собственные уведомления, а не ионный push! – Coder

-1

Я использовал http://www.plugreg.com/plugin/phonegap-build/PushPlugin развивать pushnotification и это работает хорошо.

Прежде всего, удалите любой другой плагин с уведомлением по току, а затем попробуйте этот. Он работает.

Смежные вопросы