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.
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.