Schedule a local notification 2 days before a date (selected by the user)

Hello
I have an application coded with ionic 3 which must record the date of an event, then remind the user (via local notifications) 2 days before the event and the day before the event.
when i run the code below i don’t get any error, but i also don’t see my notification when i try to list all registered notifications with this.localNotifications.getAll ();

I get the date using this: <ion-datetime displayFormat="DD MMM YYYY à HH:mm" pickerFormat="D MMMM YYYY HH:mm" [(ngModel)]="myDate" name="tokoss"></ion-datetime>

then I create the notification as well

myDate : Date ;
setNotification(){
    var date = new Date(this.myDate);
    date.setDate(date.getDay()-2); //back for 2 days
    console.log(date);
    this.localNotifications.schedule({
      id: 1,
      text: 'Single Local Notification 2 days',
      trigger: { at: new Date(date) },
      data: { secret: 'secret' }
    });
  }

how to fix this problem please

Your comment does not match what your code says, and your code does something that would appear totally random, because setDate sets absolute values and getDay gets the day of the week. What I would do is to use date-fns:

import {addDays} from "date-fns";
let twoDaysAgo = addDays(new Date(), -2);

thank you for your reply. now i can create a local notification and it runs at the scheduled time. but when i try to schedule multiple notifications, some go and some don’t. here is my code

 let date = new Date(this.myDate);
    let twoDaysAgo = addDays(date, -2);
    let oneDaysAgo = addDays(date, -1);

    // 2 days ago first notification
    twoDaysAgo.setHours(6,0,0);
    this.localNotifications.schedule({
          id: new Date().getTime(),
          text: 'local notification 2 days ago 6h',
          trigger: { at: twoDaysAgo },
          data: { secret: 'secret' }
        });

        // 2 days ago 2nd notification
        twoDaysAgo.setHours(8,0,0);
    this.localNotifications.schedule({
          id: new Date().getTime(),
          text: 'local notification 2 days ago 8h',
          trigger: { at: twoDaysAgo },
          data: { secret: 'secret' }
        });

        // 2 days ago 3rd notification
        twoDaysAgo.setHours(15,0,0);
    this.localNotifications.schedule({
          id: new Date().getTime(),
          text: 'local notification 2 days ago 15h',
          trigger: { at: twoDaysAgo },
          data: { secret: 'secret' }
        });
        // 1 days ago 1st notification
      oneDaysAgo.setHours(6,0,0);
      this.localNotifications.schedule({
        id: new Date().getTime(),
        text: 'Local notification one day ago 6h',
        trigger: { at: oneDaysAgo },
        data: { secret: 'secret' }
      });

      // 1 days ago 2nd notification
      oneDaysAgo.setHours(8,0,0);
      this.localNotifications.schedule({
        id: new Date().getTime(),
        text: 'Local notification one day ago 8h',
        trigger: { at: oneDaysAgo },
        data: { secret: 'secret' }
      });
      // 1 days ago 3rd notification
      oneDaysAgo.setHours(15,0,0);
      this.localNotifications.schedule({
        id: new Date().getTime(),
        text: 'Local notification one day ago 15h',
        trigger: { at: oneDaysAgo },
        data: { secret: 'secret' }
      });

when i run it i have the following issues:

  • the first and second notifications do not run at the scheduled time (6 a.m. for the first and 8 a.m. for the second), but run at the same time with the 3rd (scheduled for 3 p.m.)
  • when its change from “twoDayAgo” to oneDayAgo, all oneDayAgo notificatons are executed at the same time as the first (scheduled at 6h)
    please what’s the problem with my code?

When you create all of your notifications within one function and use “new Date().getTime()” for creating an ID, I guess the problem is that you will create different notifications with the same ID and that could cause your problem.

I put the id manually (1,2 …)
but the problem remains

I recommend treating Date objects as immutable. Never call any function that changes them internally. That way you don’t have to worry about aliasing effects.

Please I don’t understand what you are asking me to do.I am French-speaking and I use translation softwarecan you help me with an example of what you are asking me to do?

Never call any built-in functions on Date objects that modify the date, such as setHours. Only use functions from the date-fns library, such as set, because they always return a new Date object.