首页 > 解决方案 > 无法在点击事件时设置离子本地通知(奇怪的语法错误)

问题描述

我在我的应用程序中使用离子本地通知。到目前为止,我能够在给定时间触发通知。我还想在通知的点击事件上设置自定义操作。但是当我因为语法错误而无法这样做时。我看了很多这样的 示例代码。这是每个人用来为点击事件设置动作的通用代码。

 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();
    })
  });
}

但是由于某些未知原因,此代码对我显示语法错误

错误截图

它说

[ts] Expected 1 arguments, but got 2.
(property) HomePage.localNotifications: LocalNotifications

这是我完整的 home.ts 代码: 注意-我从 home.html 调用提交函数。通知工作正常并按时触发。我只需要在点击操作上实现自定义

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');
  }

}

标签: cordovaionic-frameworkionic3cordova-pluginslocalnotification

解决方案


正确的功能是:

this.localNotifications.on('click').subscribe(notification => {
       // Insert your logic here
        });

我在离子论坛中找到了这个答案:这里


推荐阅读