1. Agora, nós vamos fazer o serviço de criação de notificação. Nós iremos criar dentro de services um arquivo chamado “NotificationService.js”. “Src → Services → NotificationService.js”

  2. A primeira coisa que precisamos fazer é importar o expo-notifications:

    import * as Notifications from "expo-notifications";
    
  3. Agora, vamos definir uma função para criação de uma notificação, onde vamos começar recebendo alguns parâmetros pela função:

    async function createNotification(
      habitInput,
      frequencyInput,
      dayNotification,
      timeNotification
    ) {
      
    }
    
  4. Agora nós vamos pegar a hora e minutos da notificação. Nosso “timeNotification” vem com o formato padrão “00:00”, por isso nós pegamos os dois primeiros e os dois últimos dígitos e transformamos eles em números.

    async function createNotification(
      habitInput,
      frequencyInput,
      dayNotification,
      timeNotification
    ) {
      const habitHour = Number(timeNotification.slice(0, 2));
      const habitMinutes = Number(timeNotification.slice(3, 5));
    }
    
  5. Agora, nós vamos verificar o dia que foi selecionado. Se a notificação for semanal, nós vamos colocar o dia da semana como um número normal, de 1 a 7, por isso nossa variável “weekday”

    async function createNotification(
      habitInput,
      frequencyInput,
      dayNotification,
      timeNotification
    ) {
      const habitHour = Number(timeNotification.slice(0, 2));
      const habitMinutes = Number(timeNotification.slice(3, 5));
    
      let weekDay;
      if (dayNotification === "Domingo") {
        weekDay = 1;
      } else if (dayNotification === "Segunda") {
        weekDay = 2;
      } else if (dayNotification === "Terça") {
        weekDay = 3;
      } else if (dayNotification === "Quarta") {
        weekDay = 4;
      } else if (dayNotification === "Quinta") {
        weekDay = 5;
      } else if (dayNotification === "Sexta") {
        weekDay = 6;
      } else if (dayNotification === "Sábado") {
        weekDay = 7;
      }
    }
    
  6. Agora, nós vamos criar um trigger que irá guardar as informações com objeto, pois é através desse trigger que nós vamos agendar a nossa notificação.

    // ... Outros códigos	
    
      } else if (dayNotification === "Sábado") {
        weekDay = 7;
      }
    
      let triggerNotification;
    	if (frequencyInput === "Diário") {
        triggerNotification = {
          hour: habitHour,
          minute: habitMinutes,
          repeats: true,
        };
      } else if (frequencyInput === "Semanal") {
        triggerNotification = {
          repeats: true,
          weekday: weekDay,
          hour: habitHour,
          minute: habitMinutes,
        };
      }
    }
    
  7. Agora só nós resta agendar a notificação. Nós iremos colocar como corpo da notificação o nome da notificação, que o usuário registra ao criar a notificação. Nós vamos colocar o mesmo nome de notificação como identificador da notificação, para ela ser única, e não poder ter outra com o mesmo nome.

    // ... Outros códigos
    
      if (frequencyInput === "Diário") {
        triggerNotification = {
          hour: habitHour,
          minute: habitMinutes,
          repeats: true,
        };
      } else if (frequencyInput === "Semanal") {
        triggerNotification = {
          repeats: true,
          weekday: weekDay,
          hour: habitHour,
          minute: habitMinutes,
        };
      }
    
      await Notifications.scheduleNotificationAsync({
        content: {
          title: "Lembrete de hábito:",
          body: `${habitInput}`,
        },
        identifier: `${habitInput}`,
        trigger: triggerNotification,
      }).then((id) => {
        console.log(id);
      });
    }
    
  8. Por fim, só precisamos lembrar de exportar a função que criamos:

    // ... Outros códigos
    
    export default { createNotification }