-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
83 lines (70 loc) · 2.79 KB
/
index.js
File metadata and controls
83 lines (70 loc) · 2.79 KB
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
/**
* @format
*/
import { AppRegistry } from 'react-native';
import App from './src/App';
import { name as appName } from './app.json';
import notifee, { EventType, TriggerType } from '@notifee/react-native';
import BackgroundFetch from 'react-native-background-fetch';
import { notificationService } from 'src/utils/NotificationService';
import {
getExpiredMedicines,
isWithinNotificationWindow,
} from 'src/utils/getExpiredMedicines';
import { ReusableDateFormatter } from 'src/utils/FormattedDate';
import { getHealthProofileForCancelNotis } from 'src/utils/getHealthProfileForCancelNotis';
import { database } from 'src/Database/database';
import HealthProfile from 'src/Database/healthProfileModel';
import { updateHealthProfileToDone } from 'src/utils/updateHealthProfileToDone';
notifee.onBackgroundEvent(async ({ type, detail }) => {
const { notification } = detail;
// Use a switch statement to handle different event types
switch (type) {
case EventType.DISMISSED:
// User dismissed the notification
console.log('User dismissed notification', notification.id);
// You could cancel a repeating alarm here, for example
break;
case EventType.PRESS:
// User pressed the notification
console.log('User pressed notification', notification.id);
// This is often handled by the app opening, but you can add logic here
break;
case EventType.TRIGGER_NOTIFICATION_CREATED:
// This is the event that fires when your trigger notification is created.
// DO NOT display another notification here.
console.log(
'Trigger notification was created by the system',
notification.id,
);
// Perform any background tasks needed *after* the notification is shown,
// but do not display it again.
break;
}
});
const HeadlessTask = async event => {
console.log('[HeadlessTask] event:', event.taskId);
const expiredMedicines = await getExpiredMedicines();
const { flattenedNotificationIds, profileIds } =
await getHealthProofileForCancelNotis();
const isWithinNotificationTime = isWithinNotificationWindow();
for (let [index, item] of expiredMedicines.entries()) {
const { medicine_name, expiry_date } = item;
if (!isWithinNotificationTime) continue;
await notificationService(
`${medicine_name} is Expired`,
`${medicine_name} expired on ${ReusableDateFormatter(
expiry_date,
)}. Please discard or replace it.`,
);
}
if (flattenedNotificationIds.length > 0) {
await notifee.cancelTriggerNotifications(flattenedNotificationIds);
}
if (profileIds.length > 0) {
await updateHealthProfileToDone(profileIds);
}
BackgroundFetch.finish(event.taskId);
};
BackgroundFetch.registerHeadlessTask(HeadlessTask);
AppRegistry.registerComponent(appName, () => App);