-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathnotification.ts
44 lines (38 loc) · 1.21 KB
/
notification.ts
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
import { useEffect, useRef } from 'react'
import { Platform } from 'react-native'
import * as Notifications from 'expo-notifications'
export default function Notification() {
const notificationListener = useRef()
const responseListener = useRef()
useEffect(() => {
const notificationListenerCurrent = notificationListener.current
const responseListenerCurrent = responseListener.current
return () => {
Notifications.removeNotificationSubscription(notificationListenerCurrent)
Notifications.removeNotificationSubscription(responseListenerCurrent)
}
}, [])
return null
}
export async function schedulePushNotification(
weekday: number,
hour: number,
minute: number,
medication: string,
body: string,
) {
const id = await Notifications.scheduleNotificationAsync({
content: {
title: medication,
body: body,
priority: 'high',
sound: Platform.OS === 'android' ? undefined : 'default',
},
trigger: { weekday: weekday, hour: hour, minute: minute, repeats: true },
})
console.log('notif id on scheduling', id)
return id
}
export async function cancelNotification(notifId: string) {
await Notifications.cancelScheduledNotificationAsync(notifId)
}