0

I have the code below in my app's controller which does not seemed to be working. Testing it on the browser gives me this error "TypeError: Cannot read property 'plugins' of undefined". Note that the alert inside the .then is not displayed even if I emulate on a device.

angular.module('controllers', ['ionic', 'ngCordova'])

.controller('MainCtrl', function($scope, $ionicSideMenuDelegate, $ionicPlatform, $cordovaLocalNotification, GoogleMaps) {

  // Change Map Type
  $scope.setMapType = function(newMapType) {
    GoogleMaps.setMapType(newMapType);

    alert("Setting notification");

    // Supposed to trigger a notification
    $cordovaLocalNotification.schedule({
      id: 1,
      title: 'Instant',
      text: 'Instant Notification',
    }).then(function (result) {
      alert("Instant Notification set");
    });
  };
});

I've also imported ng-cordova.min.js, and cordova.js in the index.html. I also did cordova plugin add https://github.com/katzer/cordova-plugin-local-notifications.git already. Any ideas why it might not be working?

Chan Jing Hong
  • 2,251
  • 4
  • 22
  • 41
  • Check this answers about issue with cordovalocalNotification https://github.com/driftyco/ng-cordova/issues/855 – Zoran Pandovski Sep 21 '16 at 09:18
  • @Chan I would suggest you to try out this simple working sample which uses barebone cordova framwork not ionic - https://github.com/gandhirajan/Cordova_Local_Notification and build on from there – Gandhi Sep 21 '16 at 09:56

2 Answers2

0

go through this list of possible issues: http://ngcordova.com/docs/common-issues/

My suggestion would be that you will have to wrap the plugin in an ionicPlatform.ready() event.

jonas-l-b
  • 271
  • 1
  • 15
0

In browser, as the plugin will not be available, It throws plugin of undefined error.

Internally $cordovaLocalNotification.schedule will broadcast an event. You can handle using $on in your code.

 $rootScope.$on('$cordovaLocalNotification:schedule',
function (event, notification, state) {
  // ...
});

Better test it on device once. Hope this helps.

bvakiti
  • 3,243
  • 4
  • 17
  • 26