Once in a while you will need to inform application user that there’s an information they’re required to see, for example, an upcoming appointment. This is, of course, easy if said application is active and running but what if it’s running in the foreground? We can easily solve this problem with local notifications.

Note: If this tutorial was helpful, you need further clarification, something is not working or you have a request for another Ionic post? Furthermore, leave me a comment below if you don't like something about this blog, if something is bugging you, don't like how I'm doing stuff here. Feel free to comment below, subscribe to my blog, mail me to dragan.gaic@gmail.com. Thanks and have a nice day!

Table of Contents

Click here if you want to see other tutorials, or articles about the Ionic framework (themes, templates, plugins, tutorials)


Other Resources (themes, templates, plugins, Cordova plugins, starter apps)

Cordova Plugin Tutorials



Before we can start make sure you have these requirements; if you already have a configured Ionic Framework environment, skip this step completely:
  • Android Environment (or iOS if your working on a MacOS)
  • nodeJS
  • Ionic
  • Cordova
No previous Ionic Framework experience, find more information here: Ionic Framework | Installation Guide.

1. Update Ionic & Cordova

The latest nodeJs version is a top priority, make sure you have it installed. You will not be able to download/update latest Ionic Framework without it.
When you check your nodeJS version, update Ionic, and Cordova, older versions may not work with this tutorial:
npm update -g cordova ionic

2. Create a New Project

ionic start IonicLocalNotificationDemo blank
cd IonicLocalNotificationDemo
Warning: Since some of you never worked with Ionic CLI. From this point and further, every time I tell you to execute something, do that inside a project folder.
This example will use Android platform so add it to this project:
ionic platform add android
For MacOS users, do it like this:
ionic platform add ios

3. Download ngCordova archive

ngCordova is a collection of 63+ AngularJS extensions on top of the Cordova API that make it easy to build, test, and deploy Cordova mobile apps with AngularJS. While we can make this work without ngCordova, it’s much more practical this way. This collection also holds many Ionic Framework compatible plugins.
Download ngCordova zip file, here’s site link:
Official Site Docs Download Link
Unzip this archive, find a file name called: ng-cordova.min.js, it should be available at this location: ng-cordova-master\dist\.
Open your newly created project directory and copy ng-cordova.min.js to this location: IonicLocalNotificationDemo\www\js.
Now include this location to index.html located at IonicLocalNotificationDemo\www; add it above cordova.js file, like this:
<!-- cordova script (this will be a 404 during development) -->
<script src="js/ng-cordova.min.js"></script>
<script src="cordova.js"></script>

4. Install Cordova Local Notification Plugin

Add required plugins:
cordova plugin add https://github.com/katzer/cordova-plugin-local-notifications.git
cordova plugin add cordova-plugin-whitelist
Install Whitelist plugin only if you’re using Cordova 4.0 +. Without it you’ll receive “Application Error: There was a network error.” error.
GitHub - Local Notification


At this point, you should have everything set up and ready, we can start working on our example.
Go to IonicLocalNotificationDemo project directory and find app.js, alter angular.module line and include ngCordova service, like this:
var app = angular.module('starter', ['ionic', 'ngCordova'])
Open index.html file, add a controller to ion-pane directive:
      <ion-header-bar class="bar-stable">
        <h1 class="title">Local Notification Example</h1>
      <ion-content ng-controller="NotificationController">
Initialize this controller in app.js file; don’t forget to inject $cordovaLocalNotification service. We will also need $ionicPlatform, like this:
app.controller('NotificationController', function($scope, $cordovaLocalNotification, $ionicPlatform) {

If you’re using Cordova 4+ you will need to check if there’s a Whitelist plugin installed with your project. If you have it, add this security meta tag to your index.html HEAD content:
<meta http-equiv="Content-Security-Policy" content="default-src *; script-src 'self' 'unsafe-inline' 'unsafe-eval' *; style-src  'self' 'unsafe-inline' *">
Without it your application will throw an error during the app initialization, after that it will constantly throwing no security meta-tag found. While not a blocking point, constant console log warnings will make it hard to do any debugging.
We will use next function to trigger a notification, don’t forget to wrap it into Cordova deviceready event (or Ionic ready event depending on your project):
	id: 1,
	title: 'Warning',
	text: "You're so sexy!",
	data: {
	  customProperty: 'custom value'
  }).then(function (result) {
	console.log('Notification 1 triggered');
id is the most important part of this function. It’s a number that represents your local notification. Once you trigger it you can update it or cancel it through this number. In the next example, you’ll find all necessary examples.
Continue Reading