# ngCordova + local notification plugin

**URL:** <https://forum.ionicframework.com/t/ngcordova-local-notification-plugin/10534>\
**Category:** ionic-v1\
**Created:** [October 2, 2014, 10:18am UTC](https://forum.ionicframework.com/t/ngcordova-local-notification-plugin/10534 "2014-10-02T10:18:19Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![rafaellop](https://avatars.discourse-cdn.com/v4/letter/r/46a35a/32.png) [@rafaellop](https://forum.ionicframework.com/u/rafaellop)\
**Post date:** [October 2, 2014, 10:18am UTC](https://forum.ionicframework.com/t/ngcordova-local-notification-plugin/10534/1 "2014-10-02T10:18:19Z")

</div>

Hi,

I’m playing with the `$cordovaLocalNotification` plugin wrapper and I’ve found that for these two methods, Android reports errors. I mean the `isTriggered()` and `getTriggeredIds()` functions. It seems that the original plugin hasn’t got these function published, is it true?

I’d like also ask someone experienced how to use the event callback events exposed by the `$cordovaLocalNotification`. I mean the `onadd`, `ontrigger`, `onclick` and `oncancel`. I’ve just added it to the scope and they are never fired, e.g.

```auto
$cordovaLocalNotification.onadd = function (id, state, json) {
  		console.log('rp: ' + 'onadd: ' + id);
	};
  $cordovaLocalNotification.onclick = function (id, state, json) {
  		console.log('rp: ' + 'onclick: ' + id);
	};

```

Could you please point me to a right direction?

---

<div class="post-metadata">

**Author:** ![rafaellop](https://avatars.discourse-cdn.com/v4/letter/r/46a35a/32.png) [@rafaellop](https://forum.ionicframework.com/u/rafaellop)\
**Post date:** [October 2, 2014, 10:35pm UTC](https://forum.ionicframework.com/t/ngcordova-local-notification-plugin/10534/2 "2014-10-02T22:35:02Z")

</div>

One day a question, second day later the answer 😉 I should rather start a blog instead of litter here 😉  
One simple answer to my problems described above is: do not use ngCordova for local notifications. Use the plugin original examples. They work as described. ngCordova methods don’t. Simple as that. The whole magic about this plugin is:

1. add plugin using CLI (instruction: [https://github.com/katzer/cordova-plugin-local-notifications](https://github.com/katzer/cordova-plugin-local-notifications))

2. in app\_init.js or where you init your app add code that expose plugin events to the controllers. My way is:

```auto
var app = angular.module('app', ['ionic' ...])

.run(function($rootScope, $ionicPlatform, ...) {

  $ionicPlatform.ready(function() {
    if (window.cordova) {
      document.addEventListener("deviceready", function() {
        window.plugin.notification.local.onadd = app.onReminderAdd;
        window.plugin.notification.local.onclick = app.onReminderClick;
        window.plugin.notification.local.oncancel = app.onReminderCancel;
        window.plugin.notification.local.ontrigger = app.onReminderTrigger;
     }, false);
  }

app.onReminderAdd = function(id, state, json) {
  $timeout(function() {
    $rootScope.$broadcast('onReminderAdded', id, state, json);
  }, 100);
}
// other events this way as well... You can do this 
// without the timeout of course
}

```

1. Add event handlers to your selected controller e.g.

```auto
$scope.$on('onReminderAdded', function(event, id, state, json) {
  console.log('notification ADDED, id: ' + id + ' state:' + state + ' json:' + json );
});

```

1. Now add functions that add notifications, cancel, etc, and you can handle the new traffic from notifications using the events above. For example simple code for adding an immediate notification:

```auto
$scope.addNotification = function(tit, msg) {
  window.plugin.notification.local.add({
      id: 'MYLN',
      title: tit,
      message: msg,
      icon: 'ic_notification',
      smallIcon: 'ic_notification_small',
});

```

What’s worth mentioning, because it is not explained in the plugin documentation, is the icons handling procedure. The icons names in the above example points to the location: `/platforms/android/res/drawable/` To make the plugin use the icons, the image files must be located in this path and named as defined in the parameters. In my case they are:

`/platforms/android/res/drawable/ic_notification.png` and  
`/platforms/android/res/drawable/ic_notification_small.png`

By trail and error I’ve found that best sizes are 48x48 for the bigger and 24x24 for the smaller.

Happy notifying!

Rafaellop

---

<div class="post-metadata">

**Author:** ![pbernasconi](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/pbernasconi/32/7730_2.png) [@pbernasconi](https://forum.ionicframework.com/u/pbernasconi)\
**Post date:** [October 2, 2014, 10:39pm UTC](https://forum.ionicframework.com/t/ngcordova-local-notification-plugin/10534/3 "2014-10-02T22:39:13Z")

</div>

@rafaellop Hey rafael, looks great to me! If you have the time feel free to add your working solution to the [ng-cordova demo files](https://github.com/driftyco/ng-cordova/tree/master/demo/www/app)

---

<div class="post-metadata">

**Author:** ![rafaellop](https://avatars.discourse-cdn.com/v4/letter/r/46a35a/32.png) [@rafaellop](https://forum.ionicframework.com/u/rafaellop)\
**Post date:** [October 2, 2014, 10:50pm UTC](https://forum.ionicframework.com/t/ngcordova-local-notification-plugin/10534/4 "2014-10-02T22:50:54Z")

</div>

Well, in fact, I haven’t used ngCordova because it simply doesn’t work the way it stays in the documentation. It’s been faster to just start over with the original plugin. My concept works completely without the ngCordova library so I think that ngCordova repository isn’t a good place for this tutorial.

---

<div class="post-metadata">

**Author:** ![CyberFerret](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/cyberferret/32/72111_2.png) [@CyberFerret](https://forum.ionicframework.com/u/CyberFerret)\
**Post date:** [December 13, 2014, 8:33am UTC](https://forum.ionicframework.com/t/ngcordova-local-notification-plugin/10534/5 "2014-12-13T08:33:50Z")

</div>

Thanks for this - really useful tips in here, which helped me get notifications working properly in my Ionic app. I managed to use $cordovaLocalNotification for most of it, but it seemed that for the **_onclick_** event, I needed to revert back to **_window.plugin.notification.local.onclick_**.

I also managed to simplify the code in the initialisation stage. Basically had:

```
.run(function($ionicPlatform, $cordovaLocalNotification, $rootScope, DB) {

  $ionicPlatform.ready(function() {

    // Check for permissions to show local notifications - iOS 8 NEEDS permission to run!
    $cordovaLocalNotification.hasPermission().then(function(granted) {
      $cordovaLocalNotification.cancelAll();
      if (!granted) {
        $cordovaLocalNotification.promptForPermission();
      };
    });

    document.addEventListener("deviceReady", function() {
      // Event handling for Local Notifications
      window.plugin.notification.local.onclick = function(id, state, json) {
        $rootScope.$broadcast('onNotificationClick', id, state, json);
      };      
      // You can add more here for oncancel, ontrigger etc.
    });
  });
})

```

then in my controller where I wanted to handle the incoming notification response, I had the code:

```
.controller('MyCtrl', function($scope, $ionicPlatform, $cordovaLocalNotification, ) {

	$scope.$on('onNotificationClick', function(event, id, state, json) {
            // Do whatever you need with the incoming notification here...
		$cordovaLocalNotification.cancel(id);
	});    
})

```

Seems to work a treat, but I never would have got there without your help, so thanks again!

---

<div class="post-metadata">

**Author:** ![rafaellop](https://avatars.discourse-cdn.com/v4/letter/r/46a35a/32.png) [@rafaellop](https://forum.ionicframework.com/u/rafaellop)\
**Post date:** [December 13, 2014, 12:29pm UTC](https://forum.ionicframework.com/t/ngcordova-local-notification-plugin/10534/6 "2014-12-13T12:29:56Z")

</div>

@CyberFerret, great I could help. However I’ve still got some issues with the local notification feature or maybe my understanding of this feature is wrong. Do you maybe know, or anyone else, why local notifications (Android’s) that are set for a future time in a distance of e.g. a month, don’t fire a notification in the notification area? I supposed the local notifications is a system service that works independently of the application and serving events like e.g. Windows scheduler. This is a big problem for me and I cannot find a solution. So do any of you have experience with that?

---

<div class="post-metadata">

**Author:** ![happiwill](https://avatars.discourse-cdn.com/v4/letter/h/e9a140/32.png) [@happiwill](https://forum.ionicframework.com/u/happiwill)\
**Post date:** [December 29, 2014, 1:17am UTC](https://forum.ionicframework.com/t/ngcordova-local-notification-plugin/10534/7 "2014-12-29T01:17:59Z")

</div>

Has anybody got local notifications working in the simulator for ios?

---

<div class="post-metadata">

**Author:** ![happiwill](https://avatars.discourse-cdn.com/v4/letter/h/e9a140/32.png) [@happiwill](https://forum.ionicframework.com/u/happiwill)\
**Post date:** [January 11, 2015, 8:04am UTC](https://forum.ionicframework.com/t/ngcordova-local-notification-plugin/10534/8 "2015-01-11T08:04:53Z")

</div>

To reply to my own question, yes it works in simulator.

---

<div class="post-metadata">

**Author:** ![akhil](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/akhil/32/8036_2.png) [@akhil](https://forum.ionicframework.com/u/akhil)\
**Post date:** [February 9, 2015, 4:30am UTC](https://forum.ionicframework.com/t/ngcordova-local-notification-plugin/10534/9 "2015-02-09T04:30:46Z")

</div>



---

<div class="post-metadata">

**Author:** ![ionicPlay](https://avatars.discourse-cdn.com/v4/letter/i/f1d935/32.png) [@ionicPlay](https://forum.ionicframework.com/u/ionicPlay)\
**Post date:** [March 23, 2015, 9:12pm UTC](https://forum.ionicframework.com/t/ngcordova-local-notification-plugin/10534/10 "2015-03-23T21:12:45Z")

</div>

Thanks rafaellop,

I liked your approach with the $broadcast better than what I’ve implemented in the past with Push Notifications.

I was scratching my head with why ngCordova wasn’t working. I made some progress getting it to work with this post ([http://devdactic.com/local-notifications-ionic/](http://devdactic.com/local-notifications-ionic/)), but I liked you solution best.

Thanks again.

---

<div class="post-metadata">

**Author:** ![rafaellop](https://avatars.discourse-cdn.com/v4/letter/r/46a35a/32.png) [@rafaellop](https://forum.ionicframework.com/u/rafaellop)\
**Post date:** [March 24, 2015, 2:03am UTC](https://forum.ionicframework.com/t/ngcordova-local-notification-plugin/10534/11 "2015-03-24T02:03:07Z")

</div>

@ionicPlay, that’s nice 🙂 Thank you! Great article by the way.

---

<div class="post-metadata">

**Author:** ![nitishrajput01](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/nitishrajput01/32/123978_2.png) [@nitishrajput01](https://forum.ionicframework.com/u/nitishrajput01)\
**Post date:** [July 9, 2015, 8:45am UTC](https://forum.ionicframework.com/t/ngcordova-local-notification-plugin/10534/12 "2015-07-09T08:45:01Z")

</div>

@rafaellop : hey, i am using your solution but i am getting a problem  
error : ’ Cannot read property ‘notification’ of undefined ’

here i am using this code please specify what i am doing wrong

in app.js

$ionicPlatform.ready(function () {

```
    if (window.cordova) {
    document.addEventListener("deviceready", function() {
    window.plugin.notification.local.onadd = app.onReminderAdd;
    window.plugin.notification.local.onclick = app.onReminderClick;
    window.plugin.notification.local.oncancel = app.onReminderCancel;
    window.plugin.notification.local.ontrigger = app.onReminderTrigger;
 }

```

app.onReminderAdd = function(id, state, json) {  
$timeout(function() {  
$rootScope.$broadcast(‘onReminderAdded’, id, state, json);  
}, 100);  
}

In controller.js

```
 $scope.addNotification = function(tit, msg) {
  window.plugin.notification.local.add({
  id: 'MYLN',
  title: tit,
  message: msg

```

});  
};

$scope.$on(‘onReminderAdded’, function(event, id, state, json) {  
console.log(‘notification ADDED, id: ’ + id + ’ state:’ + state + ’ json:’ + json );  
});

---

<div class="post-metadata">

**Author:** ![alexweber15](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/alexweber15/32/74785_2.png) [@alexweber15](https://forum.ionicframework.com/u/alexweber15)\
**Post date:** [July 10, 2015, 2:34pm UTC](https://forum.ionicframework.com/t/ngcordova-local-notification-plugin/10534/13 "2015-07-10T14:34:03Z")

</div>

@nitishrajput01 The local notification plugin has undergone some backwards-breaking API changes recently and I don’t think ngCordova has been updated yet.

To be honest, it’s pretty simple to just use the notification plugin directly, without ngCordova:

```
$ionicPlatform.ready(function () {
  if (window.cordova && window.cordova.plugins.notification) {
    // Set default options.
    cordova.plugins.notification.local.setDefaults({
      autoCancel: false,
      badge: 1
    });

    // Ask for permission to display notifications.
    cordova.plugins.notification.local.registerPermission();

    // Listen for click event.
    cordova.plugins.notification.local.on('click', function (notification, state) {
      // Do something awesome! :smile: 
    }

    // Listen for trigger event.
    cordova.plugins.notification.local.on('trigger', function (notification, state) {
      // Do something awesome! :smile: 
    }

    // etc ...

    // To add a notification.
    cordova.plugins.notification.local.schedule({
      // options.
    });
  }
}
```

---

<div class="post-metadata">

**Author:** ![kassus](https://avatars.discourse-cdn.com/v4/letter/k/4491bb/32.png) [@kassus](https://forum.ionicframework.com/u/kassus)\
**Post date:** [July 21, 2015, 2:00pm UTC](https://forum.ionicframework.com/t/ngcordova-local-notification-plugin/10534/14 "2015-07-21T14:00:10Z")

</div>

Hello i have try like you but i don’t have any notification  
Can someone have any issue?

---

<div class="post-metadata">

**Author:** ![bradmartin](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/bradmartin/32/13210_2.png) [@bradmartin](https://forum.ionicframework.com/u/bradmartin)\
**Post date:** [July 21, 2015, 2:21pm UTC](https://forum.ionicframework.com/t/ngcordova-local-notification-plugin/10534/15 "2015-07-21T14:21:43Z")

</div>

Are you using Crosswalk?

---

<div class="post-metadata">

**Author:** ![nitishrajput01](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/nitishrajput01/32/123978_2.png) [@nitishrajput01](https://forum.ionicframework.com/u/nitishrajput01)\
**Post date:** [July 22, 2015, 5:06am UTC](https://forum.ionicframework.com/t/ngcordova-local-notification-plugin/10534/16 "2015-07-22T05:06:06Z")

</div>

Inject $cordovaLocalNotification in your controller and give permission in your app.js if your you are building on ios

and  
try this

try{

var now = new Date().getTime();  
var \_10SecondsFromNow = new Date(now + 10 \* 1000);

```
  $cordovaLocalNotification.schedule({
    id: 1,
    title: 'Title here',
    text: 'Text here',
    at: _10SecondsFromNow
  }).then(function (result) {
    // ...
  });
};

```

} catch{console.log(err);

}

---

<div class="post-metadata">

**Author:** ![filipedelmonte](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/filipedelmonte/32/90802_2.png) [@filipedelmonte](https://forum.ionicframework.com/u/filipedelmonte)\
**Post date:** [August 13, 2015, 1:54pm UTC](https://forum.ionicframework.com/t/ngcordova-local-notification-plugin/10534/17 "2015-08-13T13:54:40Z")

</div>

Is this plugin still working and compatible with android and iOS?

---

<div class="post-metadata">

**Author:** ![alexweber15](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/alexweber15/32/74785_2.png) [@alexweber15](https://forum.ionicframework.com/u/alexweber15)\
**Post date:** [August 16, 2015, 12:06pm UTC](https://forum.ionicframework.com/t/ngcordova-local-notification-plugin/10534/18 "2015-08-16T12:06:13Z")

</div>

I can confirm that the local notification cordova plugin does work on ios 7/8 devices, using it on a project at the moment. I am not, however, using ngCordova.

There’s also a more active fork going of the local notification plugin that has better ios9 support too here: [https://github.com/NorthMcCormick/cordova-plugin-local-notifications](https://github.com/NorthMcCormick/cordova-plugin-local-notifications)

---

<div class="post-metadata">

**Author:** ![filipedelmonte](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/filipedelmonte/32/90802_2.png) [@filipedelmonte](https://forum.ionicframework.com/u/filipedelmonte)\
**Post date:** [August 25, 2015, 9:41am UTC](https://forum.ionicframework.com/t/ngcordova-local-notification-plugin/10534/19 "2015-08-25T09:41:26Z")

</div>

I managed to get this working 😉 thank you for the reply.

---

<div class="post-metadata">

**Author:** ![praveenjp](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/praveenjp/32/97582_2.png) [@praveenjp](https://forum.ionicframework.com/u/praveenjp)\
**Post date:** [September 10, 2015, 7:16am UTC](https://forum.ionicframework.com/t/ngcordova-local-notification-plugin/10534/20 "2015-09-10T07:16:20Z")

</div>

Could any one tell how to set specific time in local notification.? 😒

[Next page](https://forum.ionicframework.com/t/ngcordova-local-notification-plugin/10534.md?page=2)
