Call a function from event LocalNotifications.on('click',

Hello,
I’m using LocalNotifications plugin and I set the event Click using this code:

 LocalNotifications.on('click',
      function (event, notification, state) {     
        console.log("ok");
        this.showConfirm();
      });

but I have this error: "Uncaught TypeError: this.showConfirm is not a function"
I want to show an alert so in showConfirm function (that is in the same class) I use this code:

let confirm = this.alertController.create({
              title: 'Tutto ok?',
              message: 'Sembra che sei caduto. Vuoi chiamare soccorsi?',
              buttons: [
                {
                  text: 'No',
                  handler: () => {
                    console.log('Disagree clicked');
                  }
                },
                {
                  text: 'Si',
                  handler: () => {
                    console.log('Agree clicked');
                  }
                }
              ]
    });
    confirm.present();

I also tried to put the Alert code directly in event handler of LocalNotifications but in this case alertController is undefined even if I imported it.

Can someone help me?

Thanks,
Stefano

You’re facing scoping issues

LocalNotifications.on('click',
      function (event, notification, state) {     
        console.log("ok");
        this.showConfirm();
      });

is in a different scope than the rest of the class. Rework it to this.

LocalNotifications.on('click', (event, notification, state) => {     
  console.log("ok");
  this.showConfirm();
});

note the use of the arrow function. This keeps the function scoped to the class, and not global.

3 Likes

Now It works.

Thanks

It worked for me as well, thanks for the same.