首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >重复本地通知

重复本地通知
EN

Stack Overflow用户
提问于 2020-06-01 15:19:49
回答 1查看 44关注 0票数 0

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

示例:

1-“1号房的发票将过期”

第二,“1号房的发票将过期”

第三,“2号房的发票将过期”

知道那可能是什么吗?怎么解决?

代码语言:javascript
复制
    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 }
         });
       }       
    }  
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-01 16:47:10

我认为问题在于执行calculateDif();的函数

您还可以创建已经通知的项目数组,例如notifiedHouses = [],并检查id是否已使用.some通知。

代码语言:javascript
复制
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);
       }       
    } 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62135396

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档