-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.tsx
81 lines (72 loc) · 1.96 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { useEffect, useState } from "react";
import { StatusBar } from "react-native";
import {
useFonts,
Inter_400Regular,
Inter_600SemiBold,
Inter_700Bold,
Inter_800ExtraBold,
} from "@expo-google-fonts/inter";
import * as Notifications from "expo-notifications";
import { api } from "./src/lib/api";
import { Loading } from "./src/components/Loading";
import { Routes } from "./src/routes";
import { weekDaysEUFormat } from "./src/lib/date.format";
import { getDeviceId } from "./src/lib/device.util";
const dayOfTheWeek = () => {
const today = new Date();
return weekDaysEUFormat[today.getDay()];
};
export default function App() {
const [fontsLoaded] = useFonts({
Inter_400Regular,
Inter_600SemiBold,
Inter_700Bold,
Inter_800ExtraBold,
});
const [isLoading, setIsLoading] = useState(true);
useEffect(() => {
const createOrUpdateDevice = async () => {
try {
const deviceId = await getDeviceId();
await api.post("/device", { deviceId });
setIsLoading(false);
} catch (error: any | Error) {
console.log(error.message);
}
};
createOrUpdateDevice();
}, []);
async function schedulePushNotification() {
const schedule = await Notifications.getAllScheduledNotificationsAsync();
if (schedule.length > 0) {
await Notifications.cancelAllScheduledNotificationsAsync();
}
const trigger = new Date(Date.now());
trigger.setHours(trigger.getHours() + 5);
trigger.setSeconds(0);
await Notifications.scheduleNotificationAsync({
content: {
title: `Happy ${dayOfTheWeek()}!`,
body: "Have you registered your habits today?",
},
trigger,
});
}
useEffect(() => {
schedulePushNotification();
}, []);
if (!fontsLoaded || isLoading) {
return <Loading />;
}
return (
<>
<Routes />
<StatusBar
barStyle="light-content"
backgroundColor="transparent"
translucent
/>
</>
);
}