Ionic Local Notifications "at" field changes when scheduling multiple notifications


#1

Hello,

I am using this plugin on my app

Cordova Local-Notification Plugin

For some background, my app allows the user to Bookmark a comic book series and then schedule notifications that will alert the user when there are new issues available for viewing. New comics are released every Wednesday.

To learn about local notifications, I borrowed heavily from this blog post
Getting Familiar with Local Notifications in Ionic 2 & 3

Below is my addNotification method. It sends the user to a modal where they can choose the time (Hours and Minutes) they want their notification to trigger on Wednesdays. When the modal is dismissed, a notification is created and scheduled. This notification is also pushed into an array called notifications and saved in storage.

addNotification(event, item) {

console.log('NOTIFIED');
console.log('item',item);

//Create modal to retrieve user inputed time!
let modal = this.modalCtrl.create(NotificationSettingsPage, item);

modal.onDidDismiss((data)=>{
  console.log(data);

  this.chosenHours = data.chosenHours;
  this.chosenMinutes = data.chosenMinutes;

  let currentDate = new Date();
  let currentDay = currentDate.getDay(); //Sunday = 0, Monday =1, etc.

  //We gotta notify the user every WEDNESDAY = 3 if their series gets a new issue
  
  for (let day of this.days)
  {
    //If the current Day is Wednesday
    if (currentDay === 3)
    {
      console.log('current day = '+currentDay+" day is "+day);

      let firstNotificationTime = new Date();
      let dayDifference = 0;

      firstNotificationTime.setHours(firstNotificationTime.getHours() + (24 * (dayDifference)));            
      //Will be notified at this time
      firstNotificationTime.setHours(this.chosenHours);
      firstNotificationTime.setMinutes(this.chosenMinutes);

      console.log('firstNotificationTime',firstNotificationTime)
      let notification = {
        id: day.dayCode + Math.floor(Math.random()*101),
        title: 'Hey!',
        text: 'New issues for '+item.series+'! :)',
        at: firstNotificationTime,
        every: 'week',
        data: item
      };

    this.notifications.push(notification);
    break;
    }
    else
    {
      console.log('ELSE BLOCK')
      if(currentDay === day.dayCode)
      {
        console.log('item.series',item.series);

        let firstNotificationTime = new Date();
        //let dayDifference = day.dayCode - currentDay; //Find difference in days since Wednesday
        let dayDifference = 3 - currentDay; //Find differnece in days since Wednesday
        console.log("dayDifference", dayDifference);

        if(dayDifference < 0)
        {
          dayDifference = dayDifference + 7; //for cases where the day is in the following week
        }  
        firstNotificationTime.setHours(firstNotificationTime.getHours() + (24 * (dayDifference)));
        //Will be notified at this time
        firstNotificationTime.setHours(this.chosenHours);
        firstNotificationTime.setMinutes(this.chosenMinutes);

        console.log('firstNotificationTime',firstNotificationTime)

        let notification = {
          id: day.dayCode + Math.floor(Math.random()*101),
          title: 'Hey!',
          text: 'New issues for '+item.series+'! :)',
          at: firstNotificationTime,
          every: 'week',
          data: item
        };

      this.notifications.push(notification);
      break;
      }
    }
  }
  console.log("Notifications to be scheduled: ", this.notifications);

  this.storage.set('notifications',this.notifications);
  
  if(this.platform.is('cordova')){        
    // Cancel any existing notifications
    this.localNotifications.cancelAll().then(() => {
      // Schedule the new notifications
      this.localNotifications.schedule(this.notifications);
      //this.notifications = [];
      let alert = this.alertCtrl.create({
          title: 'Notification set for '+item.series,
          buttons: ['OK']
      });
      alert.present();
    });   
  }
});

modal.present();

}

Below is a screenshot of my console output after setting a notification for the Batman series.

Console_log_1

If I schedule another notification, say for Aquaman, the at field for Batman notification gets modified and is set to the wrong date.

Why is this the case? I don’t see how I could be modifying that notification’s at field. This seems to happen with every other notification added to the array.


#2

Any ideas? I’m still at a loss.


#3

I don’t see anything wrong with your code in particular. Is there a particular reason that you’re cancelling all of them and then scheduling all of them rather than just scheduling the new notification?

Also, you don’t really need the whole loop and repeated blocks, as the only difference in each block is the dayDifference. You could simplify that to just be:

let dayDifference = 0;

if(currentDay != 3) {
  let dayDifference = 3 - currentDay;
}

if(dayDifference < 0) {
  dayDifference = dayDifference + 7; //for cases where the day is in the following week
}

That way it’s simpler code overall, and more importantly you’re not repeating yourself.


#4

SigmundFroyd,

Thanks for responding. There isn’t any specific reason why I cancelled any existing notifications. I realized I took (stole) heavily from this one post showing examples of Local Notification usage and kinda just went with it. I’m gonna schedule the new notification without cancelling existing ones and see how that goes for me.

Also, thanks for the code simplification. That came from the post I mentioned as well (the author was demonstrating how to schedule notifications for a User Selected Day of the week using checkboxes)