重复的本地通知

问题描述 投票:0回答:1

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

示例:1日-“房子#1的发票将过期”第二个-“房子#1的发票将过期”3号-“ 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 }
         });
       }       
    }  
angularjs typescript cordova ionic-framework cordova-plugins
1个回答
0
投票

我认为问题出在执行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);
       }       
    } 
© www.soinside.com 2019 - 2024. All rights reserved.