我正在开发一个具有来自数据库的值的本地通知的应用程序。但是,它重复使用相同值的通知无数次,直到更改为另一个通知。
示例:
1-“1号房的发票将过期”
第二,“1号房的发票将过期”
第三,“2号房的发票将过期”
知道那可能是什么吗?怎么解决?
calculateDif(idHouse, dateBill) {
let billDate= moment(dateBill);
var MyDate = new Date();
var MyDateString;
MyDateString = MyDate.getFullYear() + '-' + ('0' + (MyDate.getMonth()+1)).slice(-2)
+ '-' + ('0' + MyDate.getDate()).slice(-2);
let warningDate= billDate.diff(MyDateString, 'days');
if (warningDate <= 5) {
this.localNotifications.schedule({
id: 1,
text: 'The invoice of house ' idHouse + ' will expire',
sound: null,
data: { secret: 1 }
});
}
} 发布于 2020-06-01 16:47:10
我认为问题在于执行calculateDif();的函数
您还可以创建已经通知的项目数组,例如notifiedHouses = [],并检查id是否已使用.some通知。
calculateDif(idHouse, dateBill) {
let billDate= moment(dateBill);
var MyDate = new Date();
var MyDateString;
MyDateString = MyDate.getFullYear() + '-' + ('0' + (MyDate.getMonth()+1)).slice(-2)
+ '-' + ('0' + MyDate.getDate()).slice(-2);
let warningDate= billDate.diff(MyDateString, 'days');
if (warningDate <= 5 && !this.notifiedHouses.some( item => item.idHouse === idHouse )) {
this.localNotifications.schedule({
id: 1,
text: 'The invoice of house ' idHouse + ' will expire',
sound: null,
data: { secret: 1 }
});
const house = {idHouse: idHouse}
this.notifiedHouses.push(house);
}
} https://stackoverflow.com/questions/62135396
复制相似问题