0

I'm creating an Ionic 3 application and I need to schedule local notifications. I am just trying to set up a test notification but I can't seem to get it to work. I have tried in an emulator but nothing happens. The LocalNotifications plugin has also been added to my app.module.ts file.

Here's my typescript file imports and function:

 import { Component } from '@angular/core';
 import { NavController, NavParams, Platform, AlertController } from 'ionic-angular';
 import { LocalNotifications } from '@ionic-native/local-notifications';
 import * as moment from 'moment';

@Component({
   selector: 'page-notification',
   templateUrl: 'notification.html',
})
export class NotificationPage {

 // Define the variables used in the view
 notifyTime: any;
 notifications: any[] = [];


 // The constructor initializes the imports
 constructor(
          public navCtrl: NavController, 
          public navParams: NavParams,
          public platform: Platform,
          public alertController: AlertController,
          public localNotifications: LocalNotifications
          ) {}

testNotification() {

        // The notification
        let notification = {
            id:1,
            title: "test",
            text: "I am tester ?",
            every: "minute"
        };

        this.notifications.push(notification);

        if(this.platform.is('cordova')){

                // Cancel any existing notifications
                this.localNotifications.cancelAll().then(() => {

                    // Schedule the new notifications
                    this.localNotifications.schedule(this.notifications);

                    this.notifications = [];

                    let alert = this.alertController.create({
                        title: 'Notifications set',
                        buttons: ['Ok']
                    });

                    alert.present();

                });

            }



        console.log("Notifications to be scheduled: ", this.notifications);

}

I can see the following object has been passed to the console:

every:"minute"
id:1
text:"I am tester ?"
title:"test"

It still fails to show anything when the button is clicked. Do you know what to do?

kristofferandreasen
  • 847
  • 2
  • 12
  • 24

1 Answers1

2

This is how we create a simple local notification.

  1. Install the plugin.

    $ ionic plugin add --save de.appplant.cordova.plugin.local-notification
    $ npm install --save @ionic-native/local-notifications
    

incase of version 3 CLI, use

ionic cordova plugin add de.appplant.cordova.plugin.local-notification

  1. add to module.ts file

    import { LocalNotifications } from '@ionic-native/local-notifications';
    
  2. add into providers list

    providers: [
     SplashScreen,
     LocalNotifications,
     {provide: ErrorHandler, useClass: IonicErrorHandler}
    ]
    
  3. import into the page wherever we want to use and use it like this..(for demonstration purpose, I called it on viewdidload method)

    export class HomePage {
        constructor(public navCtrl: NavController,private localNotifications: 
           LocalNotifications) {}
        ionViewDidLoad() {
            console.log('ionViewDidLoad About2');
            this.localNotifications.schedule({
                    id: 1,
                    text: 'Single ILocalNotification',
                    data: 'test'
            });
         }
    }
    

and test it on real device. if you search in browser, cordova is not available so it will throw a warning like this

error

on device , result wil be

result

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
varun aaruru
  • 2,920
  • 1
  • 20
  • 36
  • 1
    Your solution works and I think mine did as well. Apparently my plugins weren't properly installed. For anyone else experiencing troubles, make sure to install the plugins in the answer and use the newest version of the Ionic CLI. – kristofferandreasen May 16 '17 at 06:50
  • @krillebimbim even i am facing "Failed to install 'de.appplant.cordova.plugin.local-notification': CordovaError: Failed to fetch plugin cordova-plugin-app-event via registry". error with installing notification plugin in ionic 3. Did you face same? How did you solve it? – Thakur Sep 13 '17 at 11:39
  • 1
    Installing with this solve my problem, "ionic cordova plugin add de.appplant.cordova.plugin.local-notification" – Zubli Quzaini Mar 12 '18 at 01:07