How can I schedule local notification specific day in every week?

Hello,

I want to make a local notification repeating some specific day in every week.
for example

  • every Monday
  • every Monday and every Tuesday.

How can I make it with LocalNotifications.schedule()?

I found there is repeats, every, and on options, but don’t know how to use them to do this.

https://capacitor.ionicframework.com/docs/apis/local-notifications#method-schedule-0

Please help me.

Thank you.

2 Likes

Suggested strategy…
On launching the app get the current date
Calculate if the current date is weekend or whichever day
If true show the notification

Thus avoiding the need to calculate future dates and store them

2 Likes

Give this a shot for example (every monday):

import { Plugins, LocalNotificationSchedule } from '@capacitor/core';
const { LocalNotifications } = Plugins;

// FOR REFERENCE YOU DONT NEED THESE COMMENT LINES AS THE INTERFACE IS IMPORTED.
// interface LocalNotificationSchedule {
//   at?: Date;
//   repeats?: boolean;
//   every?: 'year'|'month'|'two-weeks'|'week'|'day'|'hour'|'minute'|'second';
//   count?: number;
//   on?: {
//     year?: number;
//     month?: number;
//     day?: number;
//     hour?: number;
//     minute?: number;
//   }
// }

const mySchedule: LocalNotificationSchedule = {
  repeats: true,
  every: 'week',
  on: {
    day: 1
  }
};

async function setNotifications() {
  const notifs = await LocalNotifications.schedule({
    notifications: [
      {
        title: "Title",
        body: "Body",
        id: 1,
        schedule: mySchedule,
        sound: null,
        attachments: null,
        actionTypeId: "",
        extra: null
      }
    ]
  });
  console.log('scheduled notifications', notifs);
}

setNotifications();

I hope you understand this, if not let me know and I’ll help break it out more.

2 Likes

Thank you for the answer.
Do you mean that LocalNotificationSchedule.every.one.day is the index of the week?
from 0 = Sunday to 6 = Saturday?

I believe it is index the same as the JS Date indexing, so yes, Sunday - Saturday : 0 - 6

1 Like

Not working for me. If I combine every with on, the notifications won’t trigger.

3 Likes

I actually use local notification ionic capacitor. I still have the same issue. Have you already found the solution please ?

what if i want to make it repeat everyday?

For new visitors, I managed to do it this way:

on: {weekday: dayIndex}

Where dayIndex is from (1 → sunday) to (7 → saturday)

1 Like