React Native schedulePushNotification

50 Views Asked by At

I have this code to schedule a notification using expo notification

export async function schedulePushNotification( rotina, horas, minutos, dias ) {
const notifId = await Notifications.scheduleNotificationAsync({
  content: {
    title: "Rotina agendada",
    subtitle: "Mensagem de Rotinagem",
    body: `Não se esqueça, você definiu ${rotina} as ${horas}:${minutos}`,
    vibrate: true | 1000,
    priority: AndroidNotificationPriority.HIGH,
    // sound: 'default',
  },
  trigger: {
    hour: horas,
    minute: minutos,
    repeats: true,
  },
});
return notifId;

}

Now i have a list with de days, dias = [1, 2, 4, 7]

I want to set a notification in this especific days, the weekday parameter is a number in range from 1 to 7, 1 is sunday, 2 is monday...

I want if possible just one idNotification to this days, but if impossible i want a list of idNotification.

I tried but I couldn't

I've tried to pass a list in the weekDay parameter, but do not accept, and i've tried a list of idNotification, but i receive a dictionary with random letters

1

There are 1 best solutions below

0
Iman Maleki On BEST ANSWER

Each weekday should be scheduled separately.

Schedule Weekday List:

async function scheduleWeeklyReminderListAsync(
  content,
  list = [1, 2, 4, 7],
  triggerBase = { hour: 0, minute: 0, repeats: true }
) {
  const identifierList = [];

  for (const weekday of list) {
    const identifier = await Notifications.scheduleNotificationAsync({
      content,
      trigger: { ...triggerBase, weekday },
    });
    identifierList.push(identifier);
  }

  return identifierList;
}

Refactored Version:

async function scheduleWeeklyReminderListAsync(
  content,
  list = [1, 2, 4, 7],
  triggerBase = { hour: 0, minute: 0, repeats: true }
) {
  const scheduleAsync = (weekday) =>
    Notifications.scheduleNotificationAsync({
      content,
      trigger: { ...triggerBase, weekday },
    });

  return Promise.all(list.map(scheduleAsync));
}

More General Solution:

const trigger1 = { hour: 12, minute: 0, repeats: true };
const trigger2 = { hour: 8, minute: 0, repeats: true, weekday: 1 };
const trigger3 = { hour: 8, minute: 0, repeats: true, weekday: 4 };

async function scheduleReminderListAsync(
  content,
  triggerList = [trigger1, trigger2, trigger3]
) {
  const scheduleAsync = (trigger) =>
    Notifications.scheduleNotificationAsync({
      content,
      trigger,
    });

  return Promise.all(triggerList.map(scheduleAsync));
}