0
votes

I would like to create Ionic app with notifications. I installed PushPlugin and this is my controller:

.controller('MainCtrl', function($scope,$ionicPlatform,$cordovaPush) {
  $ionicPlatform.ready(function () {
    var androidConfig = {
      "senderID": "ID"
    };

      $cordovaPush.register(androidConfig).then(function(result) {
        // Success
        console.log(result);
      }, function(err) {
        // Error
      });
  })
});

After run by ionic serve I've got error:

ng-cordova.js:6180 Uncaught TypeError: Cannot read property 'pushNotification' of undefined

I found some solutions, so I've modified my code:

index.html (only head tag):

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <title></title>
    <link href="css/ionic.app.css" rel="stylesheet">

    <script src="lib/ionic/js/ionic.bundle.js"></script>

    <script src="lib/ngCordova/dist/ng-cordova.js"></script>

    <script src="cordova.js"></script>
    <!-- copy from plugin www folder -->
    <script src="js/libs/PushNotification.js"></script>

    <script src="js/app.js"></script>
    <script src="js/controllers.js"></script>
</head>

and my controller:

.controller('MainCtrl', function($scope,$ionicPlatform,$cordovaPush) {
  $ionicPlatform.ready(function () {
    var androidConfig = {
      "senderID": "ID"
    };

      if (window.plugins && window.plugins.pushNotification){
          console.log("plugins"); //it displays
          $cordovaPush.register(androidConfig).then(function(result) {
            // Success
            console.log(result);
          }, function(err) {
            // Error
          });
        }
  })
});

Now, I've got:

Uncaught ReferenceError: cordova is not defined

I'm confused, how to run it.

Plugin list:

com.phonegap.plugins.PushPlugin 2.5.0 "PushPlugin"
cordova-plugin-console 1.0.3 "Console"
cordova-plugin-device 1.1.2 "Device"
cordova-plugin-splashscreen 3.2.2 "Splashscreen"
cordova-plugin-statusbar 2.1.3 "StatusBar"
cordova-plugin-whitelist 1.2.2 "Whitelist"
ionic-plugin-keyboard 2.2.0 "Keyboard"
1

1 Answers

0
votes

Make sure of the following:

1.. Add ionic-platform-web-client and push-plugin by running the following command lines:

ionic add ionic-platform-web-client
ionic plugin add phonegap-plugin-push --variable SENDER_ID="GCM_PROJECT_NUMBER"

2.. Assign an ID to your app by running the following command line:

ionic io init

3.. Set debug to true to be able to test in browser

ionic config set dev_push true

4.. Setup your push registration in run() method

angular.module('app', ['ionic','ionic.service.core' ])
.run(function($ionicPlatform) {

    $ionicPlatform.ready(function() {
      var push = new Ionic.Push({
        "debug": true
      });

      push.register(function(token) {
        console.log("My Device token:",token.token);        
        push.saveToken(token, {'ignore_user': true}) ;  // persist the token in the Ionic Platform
      });
    }); 

})

Test your push, if it works in browser then disable the debug and start testing on your device.

ionic config set dev_push false

Complete guideline: http://docs.ionic.io/docs/push-quick-start