-
Notifications
You must be signed in to change notification settings - Fork 3
/
NotificationAdd.tsx
155 lines (151 loc) · 7.73 KB
/
NotificationAdd.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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import React, { useState } from 'react';
import { View, Text, TextInput, TouchableOpacity, PermissionsAndroid, Platform, StyleSheet, ScrollView, Switch} from 'react-native';
import { Divider } from 'react-native-paper';
import Modal from "react-native-modal";
import SwitchToggle from 'react-native-switch-toggle';
import realm from './src/localDB/document';
import * as repository from './src/localDB/document';
import DatePicker from 'react-native-date-picker';
import PushNotification from "react-native-push-notification";
import * as amplitude from './AmplitudeAPI';
const NotificationAdd = ({notificationAdded,checkNotificationAdded}:any) => {
const [isModalVisible, setIsModalVisible] = useState(false);
const [date, setDate] = useState(new Date());
const [isModalNoticeVisible, setIsModalNoticeVisible] = useState(false);
//console.log(time);
//date.setHours(hour);
//date.setMinutes(minute);
return (
<View>
<TouchableOpacity onPress={() => {
amplitude.intoAddNewNoti();
setIsModalVisible(!isModalVisible);
}}>
<View style={{
paddingHorizontal: 35,
paddingBottom: 20,
paddingTop: 20,
flexDirection: 'row',
justifyContent: 'space-between'
}}>
<Text style={{fontSize: 17}}>+ 알림 추가</Text>
</View>
</TouchableOpacity>
<Modal isVisible={isModalVisible}
animationIn={"fadeIn"}
animationInTiming={200}
animationOut={"fadeOut"}
animationOutTiming={200}
onBackdropPress={() => {
amplitude.cancelNewNoti();
setIsModalVisible(!isModalVisible);
}}
backdropColor='#CCCCCC'//'#FAFAFA'
backdropOpacity={0.8}
style={{
alignItems:'center'
}}>
<View style={{
backgroundColor:"#FFFFFF",
width:340,
height:340,
paddingHorizontal: 20,
paddingBottom: 20,
paddingTop: 20,
//justifyContent:'center',
//alignItems:'center',
borderRadius:10
}}>
<View style={{
paddingBottom: '10%',
}}>
<Text style={{fontSize: 17, color:"#495057"}}>알림 추가</Text>
</View>
<View style={{
paddingBottom: 10,
alignItems:'center',
}}>
<DatePicker date={date} onDateChange={(changedDate) => {
setDate(changedDate);
}} mode="time"
theme="light"/>
</View>
<View style={{
paddingHorizontal: "15%",
padding:'8%',
flexDirection: 'row',
justifyContent: 'space-between'
}}>
<TouchableOpacity onPress={()=>{
const notificationTime=String(date.getHours()).padStart(2,'0')+':'+String(date.getMinutes()).padStart(2,'0');
console.log(notificationTime);
console.log("**********");
amplitude.saveNewNoti(notificationTime);
if(repository.getNotificationsByField("time",notificationTime)===undefined){
realm.write(() => {repository.createNotification({
day: [true, true, true, true, true, false, false],
time: notificationTime
});});
//console.log(changedDate);
console.log(date);
if(date.getTime()<=(new Date(Date.now())).getTime()) date.setDate(date.getDate()+1);
PushNotification.localNotificationSchedule({
channelId: "MoodMemo_ID",
smallIcon: "ic_notification",
message: notificationTime+' 알림',
date: date, //입력 받은 시간으로 알림 설정
visibility: "public",
playSound: false,
id: String(date.getHours()).padStart(2,'0')+String(date.getMinutes()).padStart(2,'0'), //알림의 id는 0000 형식, 앞의 두 개는 시간, 뒤의 두 개는 분임, 시간이 겹치는 알림은 존재하지 않도록 알림 생성 이전에 처리함.
repeatType: "day",
repeatTime: "1" //하루 단위로 반복
});
checkNotificationAdded(!notificationAdded);//알림 추가 시 알림 목록 모달 리렌더링
console.log(repository.getAllNotifications());
setIsModalVisible(!isModalVisible);//저장 눌렀으면 모달이 꺼지도록
}
else{
setIsModalNoticeVisible(!isModalNoticeVisible);
}
}}>
<Text style={{fontSize: 17}}>저장</Text>
</TouchableOpacity>
<TouchableOpacity onPress={()=>{
amplitude.cancelNewNoti();
setIsModalVisible(!isModalVisible);
}}>
<Text style={{fontSize: 17}}>취소</Text>
</TouchableOpacity>
</View>
</View>
</Modal>
<Modal isVisible={isModalNoticeVisible}
animationIn={"fadeIn"}
animationInTiming={200}
animationOut={"fadeOut"}
animationOutTiming={200}
onBackdropPress={() => {
amplitude.saveDuplicatedNoti();
setIsModalNoticeVisible(!isModalNoticeVisible);
}}
backdropColor='#CCCCCC'//'#FAFAFA'
backdropOpacity={0.5}
style={{
alignItems:'center'
}}>
<View style={{
backgroundColor:"#FFFFFF",
width:'50%',
height:'20%',
justifyContent:'center',
alignItems:'center',
borderRadius:10
}}>
<Text style={{fontSize: 17}}>이미 해당 시간에</Text>
<Text style={{fontSize: 17}}>알림이 있어요!</Text>
</View>
</Modal>
</View>
);
};
export default NotificationAdd;