-
Notifications
You must be signed in to change notification settings - Fork 95
/
index.js
175 lines (142 loc) · 4.47 KB
/
index.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import { NativeModules } from 'react-native';
const { RNAlarmNotification } = NativeModules;
const ReactNativeAN = {};
const parseDateString = (string) => {
const splits = string.split(' ');
const dateSplits = splits[0].split('-');
const timeSplits = splits[1].split(':');
const year = dateSplits[2];
const month = dateSplits[1];
const day = dateSplits[0];
const hours = timeSplits[0];
const minutes = timeSplits[1];
const seconds = timeSplits[2];
return new Date(year, month - 1, day, hours, minutes, seconds);
};
ReactNativeAN.scheduleAlarm = async (details) => {
if (!details.fire_date || (details.fire_date && details.fire_date === '')) {
throw new Error('failed to schedule alarm because fire date is missing');
}
const past = parseDateString(details.fire_date);
const today = new Date();
if (past < today) {
throw new Error(
'failed to schedule alarm because fire date is in the past'
);
}
const repeatInterval = details.repeat_interval || 'hourly';
const intervalValue = details.interval_value || 1;
if (isNaN(intervalValue)) {
throw new Error('interval value should be a number');
}
if (
repeatInterval === 'minutely' &&
(intervalValue < 1 || intervalValue > 59)
) {
throw new Error('interval value should be between 1 and 59 minutes');
}
if (
repeatInterval === 'hourly' &&
(intervalValue < 1 || intervalValue > 23)
) {
throw new Error('interval value should be between 1 and 23 hours');
}
const data = {
...details,
has_button: details.has_button || false,
vibrate: (typeof details.vibrate === 'undefined' ? true : details.vibrate),
play_sound: (typeof details.play_sound === 'undefined' ? true : details.play_sound),
schedule_type: details.schedule_type || 'once',
repeat_interval: details.repeat_interval || 'hourly',
interval_value: details.interval_value || 1,
volume: details.volume || 0.5,
sound_name: details.sound_name || '',
snooze_interval: details.snooze_interval || 1,
data: details.data || '',
loop_sound: details.loop_sound || false,
};
return await RNAlarmNotification.scheduleAlarm(data);
};
ReactNativeAN.sendNotification = (details) => {
const data = {
...details,
has_button: false,
vibrate: (typeof details.vibrate === 'undefined' ? true : details.vibrate),
play_sound: (typeof details.play_sound === 'undefined' ? true : details.play_sound),
schedule_type: details.schedule_type || 'once',
volume: details.volume || 0.5,
sound_name: details.sound_name || '',
snooze_interval: details.snooze_interval || 1,
data: details.data || '',
};
RNAlarmNotification.sendNotification(data);
};
ReactNativeAN.deleteAlarm = (id) => {
if (!id) {
throw new Error('id is required to delete alarm');
}
RNAlarmNotification.deleteAlarm(id);
};
ReactNativeAN.deleteRepeatingAlarm = (id) => {
if (!id) {
throw new Error('id is required to delete alarm');
}
RNAlarmNotification.deleteRepeatingAlarm(id);
};
ReactNativeAN.stopAlarmSound = () => {
return RNAlarmNotification.stopAlarmSound();
};
ReactNativeAN.removeFiredNotification = (id) => {
if (!id) {
throw new Error('id is required to remove notification');
}
RNAlarmNotification.removeFiredNotification(id);
};
ReactNativeAN.removeAllFiredNotifications = () => {
RNAlarmNotification.removeAllFiredNotifications();
};
ReactNativeAN.getScheduledAlarms = async () => {
return await RNAlarmNotification.getScheduledAlarms();
};
// ios request permission
ReactNativeAN.requestPermissions = async (permissions) => {
let requestedPermissions = {
alert: true,
badge: true,
sound: true,
};
if (permissions) {
requestedPermissions = {
alert: !!permissions.alert,
badge: !!permissions.badge,
sound: !!permissions.sound,
};
}
return await RNAlarmNotification.requestPermissions(requestedPermissions);
};
// ios check permission
ReactNativeAN.checkPermissions = (callback) => {
RNAlarmNotification.checkPermissions(callback);
};
ReactNativeAN.parseDate = (rawDate) => {
let hours;
let day;
let month;
if (rawDate.getHours().toString().length === 1) {
hours = `0${rawDate.getHours()}`;
} else {
hours = `${rawDate.getHours()}`;
}
if (rawDate.getDate().toString().length === 1) {
day = `0${rawDate.getDate()}`;
} else {
day = `${rawDate.getDate()}`;
}
if (rawDate.getMonth().toString().length === 1) {
month = `0${rawDate.getMonth() + 1}`;
} else {
month = `${rawDate.getMonth() + 1}`;
}
return `${day}-${month}-${rawDate.getFullYear()} ${hours}:${rawDate.getMinutes()}:${rawDate.getSeconds()}`;
};
export default ReactNativeAN;