重复本地通知

Repeated local notifications

我正在开发一个带有本地通知的应用程序,其值来自数据库。但是,它会无数次重复具有相同值的通知,直到更改为另一个。

示例:
第一 - "The invoice of house #1 will expire"
第二 - "The invoice of house #1 will expire"
第三 - "The invoice of house #2 will expire"

知道那可能是什么以及如何解决它吗?

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

我认为问题出在执行 calculateDif();

的函数中

您还可以创建一个您已经通知的文章数组,例如notifiedHouses = [] 并检查是否已使用 .some

通知 id
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);
       }       
    }