【问题标题】:Repeated local notifications重复的本地通知
【发布时间】:2020-09-19 22:35:27
【问题描述】:

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

示例:
1st - “房子 #1 的发票将过期”
2nd - “房子 #1 的发票将过期”
3rd - “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】:

    我认为问题出在执行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);
           }       
        } 
    

    【讨论】:

    • 对我来说听起来是一个有趣的解决方案,我会对其进行测试并在测试后提供反馈。感谢贡献
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多