Ionic 2
Hello, I want to schedule more than one local notification in my device. I change the ID of the notification, but when I schedule more than one the last one replace the firts notification. This is my code source
import { Component } from ‘@angular/core’;
import { NavController, Platform, AlertController } from ‘ionic-angular’;
import { LocalNotifications } from ‘@ionic-native/local-notifications’;
import * as moment from ‘moment’;
@Component({
selector: ‘page-home’,
templateUrl: ‘home.html’
})
export class HomePage {
notifyTime: any;
//Aregglo de notificaciones
notifications: any[] = [];
//Dia
days: any[];
//Hora de entrada
chosenHours: number;
chosenMinutes: number;
notification: any = [];
constructor(public navCtrl: NavController, public platform: Platform, public alertCtrl: AlertController, public localNotifications: LocalNotifications) {
this.notifyTime = moment(new Date()).format();
this.chosenHours = new Date().getHours();
this.chosenMinutes = new Date().getMinutes();
this.days = [
{title: 'Monday', dayCode: 1, checked: false},
{title: 'Tuesday', dayCode: 2, checked: false},
{title: 'Wednesday', dayCode: 3, checked: false},
{title: 'Thursday', dayCode: 4, checked: false},
{title: 'Friday', dayCode: 5, checked: false},
{title: 'Saturday', dayCode: 6, checked: false},
{title: 'Sunday', dayCode: 0, checked: false}
];
}
timeChange(time){ // Seleccinar los minutos
this.chosenHours = time.hour;
this.chosenMinutes = time.minute;
}
addNotifications(){
let currentDate = new Date();
let currentDay = currentDate.getDay(); // Sunday = 0, Monday = 1, etc.
for(let day of this.days){
if(day.checked){
let firstNotificationTime = new Date();
let dayDifference = day.dayCode - currentDay;
if(dayDifference < 0){
dayDifference = dayDifference + 7; // for cases where the day is in the following week
}
firstNotificationTime.setHours(firstNotificationTime.getHours() + (24 * (dayDifference)));
firstNotificationTime.setHours(this.chosenHours);
firstNotificationTime.setMinutes(this.chosenMinutes);
let notification = {
id: day.dayCode,
title: 'Hey!',
text: 'You just got notified :)',
at: firstNotificationTime,
every: 'week'
};
this.notifications.push(notification);
}
}
console.log("Notifications to be scheduled: ", 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: 'Notificacion establecida',
buttons: ['Ok']
});
alert.present();
});
}
}
vernotifi(){
this.localNotifications.getAll().then(function(notification) {
console.log(“AQUI!!!//////////////”);
console.log(JSON.stringify(notification));
});
}
cancelAll(){
this.localNotifications.cancelAll();
let alert = this.alertCtrl.create({
title: 'Notifications cancelled',
buttons: ['Ok']
});
alert.present();
}
}