Search code examples
cordovaionic3angular5localnotification

Issue on implementing local notifications in ionic3


I am trying to add notifications to my app but it seems that nothing is working. I have added the imports in the app module as well as in the page I am implementing the notification .I expected to see notification but after clicking the button nothing is appearing.

import { LocalNotifications } from '@ionic-native/local-notifications';

   play(){
       this.localNotifications.schedule({
                id: 1,
                text: 'Hello there',
                data: 'notified'
        });
   }

Code for the button:

  <button class="login-button" ion-button round color="secondary"(click)="play()">NOTIFICATION</button>

Solution

  • Assuming that you install and configure properly, try the below steps

    Step # 1 ionic cordova plugin add cordova-plugin-local-notification npm install @ionic-native/local-notifications

    Step # 2 You will also need to add the plugin as a provider in src/app/app.module.ts

     providers: [
        StatusBar,
        SplashScreen,
        LocalNotifications,
        {provide: ErrorHandler, useClass: IonicErrorHandler}
      ]
    

    May be you forget to add in app.module

    Step # 3

    Import { LocalNotifications } from '@ionic-native/local-notifications/ngx';
    
    
    constructor(private localNotifications: LocalNotifications) { }
    
    ...
    
    
    // Schedule a single notification
    this.localNotifications.schedule({
      id: 1,
      text: 'Single ILocalNotification',
      sound: isAndroid? 'file://sound.mp3': 'file://beep.caf',
      data: { secret: key }
    });