How to use Local Notifications with Ionic Framework

How to use Local Notifications with Ionic Framework

 

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, need further clarification, something is not working or do you have a request for another Ionic post? Furthermore, if you don’t like something about this blog, if something is bugging you, don’t like how I’m doing stuff here, again leave me a comment below. I’m here to helpyou, I expect the same from you. Feel free to comment below, subscribe to my blog, mail me todragan.gaic@gmail.com, or follow and mention me on twitter (@gajotres). Thanks and have a nice day!

PS. If you want my help, if possible (even if it takes you some time to do that), create a working example I can play with. Use Plunker for AngularJS based questions or jsFiddle for jQuery/jQuery Mobile based questions.

 

TABLE OF CONTENTS

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

PREPARATION

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:

1
npm update -g cordova ionic

2. CREATE A NEW PROJECT

1
2
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:

1
ionic platform add android

For MacOS users, do it like this:

1
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:

1
2
3
<!-- 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:

1
2
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

DEVELOPMENT

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:

1
var app = angular.module('starter', ['ionic', 'ngCordova'])

Open index.html file, add a controller to ion-pane directive:

1
2
3
4
5
6
7
<ion-pane>
    <ion-header-bar class="bar-stable">
      <h1 class="title">Local Notification Example</h1>
    </ion-header-bar>
    <ion-content ng-controller="NotificationController">
    </ion-content>
 </ion-pane>

Initialize this controller in app.js file; don’t forget to inject $cordovaLocalNotification service. We will also need $ionicPlatform, like this:

1
2
3
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:

1
<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 devicereadyevent (or Ionic ready event depending on your project):

1
2
3
4
5
6
7
8
9
10
$cordovaLocalNotification.schedule({
  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
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s