-
Notifications
You must be signed in to change notification settings - Fork 0
/
Calender.js
140 lines (117 loc) · 4.66 KB
/
Calender.js
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { StatusBar } from 'expo-status-bar';
import React, {useEffect, useState, useRef} from 'react';
import { StyleSheet, Text, Button, TextInput, View, ImageBackground, TouchableOpacity } from 'react-native';
import * as Notifications from 'expo-notifications';
import Constants from 'expo-constants';
import storage from "@react-native-async-storage/async-storage";
Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldShowAlert: true,
shouldPlaySound: true,
shouldSetBadge: true
})
});
export default function Calendar() {
const [notification, setNotification] = useState(false);
const notificationListener = useRef();
const responseListener = useRef();
const [milesPerWeek, setMilesPerWeek] = useState(null);
const [nextMaintenanceDate, setNextMaintenanceDate] = useState(null);
const [nextMaintenanceMileage, setMilesToNext] = useState(null);
const calculateNextMaintenanceDate = () => {
// Calculate the next expected maintenance date based on the user input
const milesPerYear = milesPerWeek * 52;
const weeksUntilNextMaintenance = Math.ceil(nextMaintenanceMileage / milesPerYear * 52);
const nextMaintenanceDate = new Date();
nextMaintenanceDate.setDate(nextMaintenanceDate.getDate() + weeksUntilNextMaintenance * 7);
setNextMaintenanceDate(nextMaintenanceDate.toDateString());
};
useEffect(() => {
const getPermission = async () => {
if (Constants.isDevice) {
const { status: existingStatus } = await Notifications.getPermissionsAsync();
let finalStatus = existingStatus;
if (existingStatus !== 'granted') {
const { status } = await Notifications.requestPermissionsAsync();
finalStatus = status;
}
if (finalStatus !== 'granted') {
alert('Enable push notifications to use the app!');
await storage.setItem('expopushtoken', "");
return;
}
const token = (await Notifications.getExpoPushTokenAsync()).data;
await storage.setItem('expopushtoken', token);
} else {
alert('Must use physical device for Push Notifications');
}
if (Platform.OS === 'android') {
Notifications.setNotificationChannelAsync('default', {
name: 'default',
importance: Notifications.AndroidImportance.MAX,
vibrationPattern: [0, 250, 250, 250],
lightColor: '#FF231F7C',
});
}
}
getPermission();
notificationListener.current = Notifications.addNotificationReceivedListener(notification => {
setNotification(notification);
});
responseListener.current = Notifications.addNotificationResponseReceivedListener(response => {});
return () => {
Notifications.removeNotificationSubscription(notificationListener.current);
Notifications.removeNotificationSubscription(responseListener.current);
};
}, []);
const onClick = async () => {
await Notifications.scheduleNotificationAsync({
content: {
title: "Car Cat",
body: 'You have a maintenance coming up!',
data: { data: "data goes here" }
},
trigger: {
seconds:5,
//Date: nextMaintenanceDate
}
});
}
return (
<View style={styles.container}>
<TextInput
value={nextMaintenanceMileage}
onChangeText={setMilesToNext}
keyboardType="numeric"
placeholder="HOW MANY MILES UNTIL INSPECTION?"
style={{ borderWidth: 1, borderColor: 'black', padding: 10, marginBottom: 20 }}
/>
<TextInput
value={milesPerWeek}
onChangeText={setMilesPerWeek}
keyboardType="numeric"
placeholder="HOW MANY MILES DO YOU DRIVE PER WEEK?"
style={{ borderWidth: 1, borderColor: 'black', padding: 10, marginBottom: 10 }}
/>
<Button title="Calculate Next Maintenance Date" onPress={calculateNextMaintenanceDate} />
{nextMaintenanceDate && (
<Text style={{ marginTop: 20, marginBottom:20}}>Next Maintenance Date: {nextMaintenanceDate}</Text>
)}
<TouchableOpacity onPress={onClick}>
<Text style={{backgroundColor: 'black', padding: 20, marginBottom:100, color: 'white'}}>Click me to schedule a notification!</Text>
</TouchableOpacity>
<StatusBar style="auto" />
</View>
);
}
const styles = StyleSheet.create({
backgroundImage: {
flex: 1,
},
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});