/
notifications.js
124 lines (102 loc) · 4.12 KB
/
notifications.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
import PushNotification, { Importance } from 'react-native-push-notification';
import AsyncStorage from '@react-native-async-storage/async-storage';
const NOTIFICATION_STORAGE_KEY = 'NOTIFICATION_STORAGE_KEY';
PushNotification.configure({
// (optional) Called when Token is generated (iOS and Android)
onRegister: function (token) {
console.log('TOKEN:', token);
},
// (required) Called when a remote is received or opened, or local notification is opened
onNotification: function (notification) {
console.log('NOTIFICATION:', notification);
// process the notification
},
// (optional) Called when Registered Action is pressed and
// invokeApp is false, if true onNotification will be called(Android)
onAction: function (notification) {
console.log('ACTION:', notification.action);
console.log('NOTIFICATION:', notification);
// process the action
},
// (optional) Called when the user fails to register for remote notifications. Typically occurs when APNS is having issues, or the device is a simulator. (iOS)
onRegistrationError: function (err) {
console.error(err.message, err);
},
popInitialNotification: true,
requestPermissions: Platform.OS === 'ios',
});
PushNotification.createChannel(
{
channelId: 'study-remainder-channel', // (required)
channelName: 'Studt Remainders', // (required)
channelDescription: 'Study Remainder', // (optional) default: undefined.
playSound: true, // (optional) default: true
soundName: 'default', // (optional) See `soundName` parameter of `localNotification` function
importance: Importance.HIGH, // (optional) default: Importance.HIGH. Int value of the Android notification importance
vibrate: true, // (optional) default: true. Creates the default vibration patten if true.
},
(created) => console.log(`createChannel returned '${created}'`) // (optional) callback returns whether the channel was created, false means it already existed.
);
//------------------------------------------------------------------
// export const testNotification = () => {
// PushNotification.localNotification({
// channelId: 'study-remainder-channel',
// title: 'Time to study',
// message: 'Do not forget to study today!',
// });
// };
// export const testRemoveAllDeliveredNotifications = () => {
// PushNotification.removeAllDeliveredNotifications();
// };
export const setNotificationForTomorrow = async () => {
// register new notification if there's one already.
const registeredNotification = await getRegisteredNotification();
if (registeredNotification === null) {
const tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
tomorrow.setHours(20);
tomorrow.setMinutes(0);
PushNotification.localNotificationSchedule({
channelId: 'study-remainder-channel',
title: 'Time to study',
message: 'Do not forget to study today!',
date: new Date(tomorrow),
allowWhileIdle: false,
repeatType: 'day',
});
await storeRegisteredNotification();
}
};
export const cancelAllNotification = async () => {
PushNotification.cancelAllLocalNotifications();
await removeRegisteredNotification();
};
export const reschedualeForTomrrow = async () => {
await cancelAllNotification();
await setNotificationForTomorrow();
};
//----------------------------------------------------------------
// Local Storage API
const getRegisteredNotification = async () => {
try {
const result = await AsyncStorage.getItem(NOTIFICATION_STORAGE_KEY);
return result !== null ? JSON.parse(result) : null;
} catch (e) {
console.error("Can't get registered notification, ", e);
}
};
const storeRegisteredNotification = async (title) => {
try {
await AsyncStorage.setItem(NOTIFICATION_STORAGE_KEY, JSON.stringify(true));
} catch (e) {
console.error("Can't store registered notification, ", e);
}
};
const removeRegisteredNotification = async (title) => {
try {
await AsyncStorage.removeItem(NOTIFICATION_STORAGE_KEY);
} catch (e) {
console.error("Can't remove registered notification, ", e);
}
};
//----------------------------------------------------------------