Repeated local notifications

Issue

I am developing an application with local notifications with values coming from the database. However, it is repeating the notification with the same value countless times until changing to another.

Example:
1st – “The invoice of house #1 will expire”
2nd – “The invoice of house #1 will expire”
3rd – “The invoice of house #2 will expire”

Any idea what that might be and how to fix it?

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

Solution

I think the problem is in the function that execute calculateDif();

You can also create an array of your articles that you have already notified, for example notifiedHouses = []
and check if the id is already notified using .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);
       }       
    } 

Answered By – tobe_ehu

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published