I am using Ionic Local Notifications in my app. So far I am able to trigger a notification at a given time. I also want to do set a custom action to on click event of notification. But when I am not able to do so because of syntax error. I looked at many example codes like this one. This is the general code that everyone is using to set an action for click event.
this.plt.ready().then((readySource) => {
this.localNotifications.on('click', (notification, state) => {
let json = JSON.parse(notification.data);
let alert = alertCtrl.create({
title: notification.title,
subTitle: json.mydata
});
alert.present();
})
});
}
But for some unknown reason, this code shows syntax error for me
It says
[ts] Expected 1 arguments, but got 2.
(property) HomePage.localNotifications: LocalNotifications
Here is my full home.ts code: Note- I am calling the submit function from home.html. Notifications are working perfectly and triggered on time. I just need to implement custom on click action.
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, Platform, AlertController } from 'ionic-angular';
import { LocalNotifications } from '@ionic-native/local-notifications';
/**
* Generated class for the HomePage page.
*
* See https://ionicframework.com/docs/components/#navigation for more info on
* Ionic pages and navigation.
*/
@IonicPage()
@Component({
selector: 'page-home',
templateUrl: 'home.html',
})
export class HomePage {
data = { title: '', description: '', date: '', time: '' };
constructor(public navCtrl: NavController, private localNotifications: LocalNotifications, public navParams: NavParams, public platform: Platform,
public alertCtrl: AlertController) {
this.platform.ready().then((readySource) => {
this.localNotifications.on('click', (notification, state) => {
let alert = this.alertCtrl.create({
title: 'Notification Clicked'
});
alert.present();
});
});
}
submit() {
console.log(this.data);
var date = new Date(this.data.date + " " + this.data.time);
var title = this.data.title;
var text = this.data.description;
console.log(date);
this.localNotifications.schedule({
title: title,
text: text,
trigger: {at: date},
led: 'FF0000',
sound: this.setSound(),
actions: [
{ id: 'yes', title: 'Yes' },
{ id: 'no', title: 'No' }
]
});
}
setSound() {
if (this.platform.is('android')) {
return 'file://assets/sounds/Rooster.mp3'
} else {
return 'file://assets/sounds/Rooster.caf'
}
}
ionViewDidLoad() {
console.log('ionViewDidLoad HomePage');
}
}