cordova - 无法在点击事件时设置离子本地通知(奇怪的语法错误)
问题描述
我在我的应用程序中使用离子本地通知。到目前为止,我能够在给定时间触发通知。我还想在通知的点击事件上设置自定义操作。但是当我因为语法错误而无法这样做时。我看了很多这样的 示例代码。这是每个人用来为点击事件设置动作的通用代码。
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');
}
}
解决方案
正确的功能是:
this.localNotifications.on('click').subscribe(notification => {
// Insert your logic here
});
我在离子论坛中找到了这个答案:这里
推荐阅读
- css - 如何将下面的 div 放在另一个 div 中(与另一个 div 之间重叠)?
- laravel - 缺少 Zip 扩展名(作曲家更新)
- python - 执行大点/张量点积的最有效方法,同时只保留对角线条目
- raspberry-pi - Raspberry Pi 未收到 Alexa M-search
- jax-rs - 以向后兼容的方式为 JerseyClient 将 @QueryParam 添加到 JAX-RS 现有 API
- css - 当我从 nativescript 游乐场下载时运行 app.css 时出错
- java - 计算六边形图libgdx的大小
- mysql - 在依赖于顺序响应的 MySQL 的两个查询之间注入 API 调用
- python - 你如何创建一个包含所有行和值的新变量?
- angular - 如何告诉角度不更新视图