2025-04-21 15:41:08 +00:00
|
|
|
|
|
|
|
|
import { useState, useEffect, useRef } from 'react';
|
|
|
|
|
import { Platform } from 'react-native';
|
|
|
|
|
import * as Device from 'expo-device';
|
|
|
|
|
import * as Notifications from 'expo-notifications';
|
|
|
|
|
import Constants from 'expo-constants';
|
|
|
|
|
|
|
|
|
|
Notifications.setNotificationHandler({
|
|
|
|
|
handleNotification: async () => ({
|
|
|
|
|
shouldShowAlert: true,
|
|
|
|
|
shouldPlaySound: false,
|
|
|
|
|
shouldSetBadge: false,
|
|
|
|
|
}),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const schedulePushNotification = async (title, body, dataObj) => {
|
|
|
|
|
await Notifications.scheduleNotificationAsync({
|
|
|
|
|
content: {
|
|
|
|
|
title,
|
|
|
|
|
body,
|
|
|
|
|
data: { data: dataObj },
|
|
|
|
|
},
|
|
|
|
|
trigger: {
|
|
|
|
|
type: Notifications.SchedulableTriggerInputTypes.TIME_INTERVAL,
|
|
|
|
|
seconds: 2,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const registerForPushNotificationsAsync = async () => {
|
|
|
|
|
let token;
|
|
|
|
|
|
|
|
|
|
if (Platform.OS === 'android') {
|
|
|
|
|
await Notifications.setNotificationChannelAsync('myNotificationChannel', {
|
|
|
|
|
name: 'A channel is needed for the permissions prompt to appear',
|
|
|
|
|
importance: Notifications.AndroidImportance.MAX,
|
|
|
|
|
vibrationPattern: [0, 250, 250, 250],
|
|
|
|
|
lightColor: '#FF231F7C',
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Device.isDevice) {
|
|
|
|
|
const { status: existingStatus } = await Notifications.getPermissionsAsync();
|
|
|
|
|
let finalStatus = existingStatus;
|
|
|
|
|
if (existingStatus !== 'granted') {
|
2025-04-21 22:04:03 +00:00
|
|
|
const { status } = await Notifications.requestPermissionsAsync();
|
|
|
|
|
finalStatus = status;
|
2025-04-21 15:41:08 +00:00
|
|
|
}
|
|
|
|
|
if (finalStatus !== 'granted') {
|
2025-04-21 22:04:03 +00:00
|
|
|
alert('Failed to get push token for push notification!');
|
2025-04-21 15:41:08 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// Learn more about projectId:
|
|
|
|
|
// https://docs.expo.dev/push-notifications/push-notifications-setup/#configure-projectid
|
|
|
|
|
// EAS projectId is used here.
|
|
|
|
|
try {
|
2025-04-21 22:04:03 +00:00
|
|
|
const projectId =
|
|
|
|
|
Constants?.expoConfig?.extra?.eas?.projectId ?? Constants?.easConfig?.projectId;
|
|
|
|
|
if (!projectId) {
|
|
|
|
|
throw new Error('Project ID not found');
|
|
|
|
|
}
|
|
|
|
|
token = (
|
2025-04-21 15:41:08 +00:00
|
|
|
await Notifications.getExpoPushTokenAsync({
|
2025-04-21 22:04:03 +00:00
|
|
|
projectId,
|
2025-04-21 15:41:08 +00:00
|
|
|
})
|
2025-04-21 22:04:03 +00:00
|
|
|
).data;
|
2025-04-21 15:41:08 +00:00
|
|
|
} catch (e) {
|
2025-04-21 22:04:03 +00:00
|
|
|
token = `${e}`;
|
2025-04-21 15:41:08 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
alert('Must use physical device for Push Notifications');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return token;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const useNotifications = () => {
|
|
|
|
|
const [expoPushToken, setExpoPushToken] = useState('');
|
|
|
|
|
const [channels, setChannels] = useState([]);
|
|
|
|
|
const [notification, setNotification] = useState(undefined);
|
|
|
|
|
const notificationListener = useRef();
|
|
|
|
|
const responseListener = useRef();
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
registerForPushNotificationsAsync().then(token => token && setExpoPushToken(token));
|
|
|
|
|
|
|
|
|
|
if (Platform.OS === 'android') {
|
|
|
|
|
Notifications.getNotificationChannelsAsync().then(value => setChannels(value ?? []));
|
|
|
|
|
}
|
|
|
|
|
notificationListener.current = Notifications.addNotificationReceivedListener(notification => {
|
|
|
|
|
setNotification(notification);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
responseListener.current = Notifications.addNotificationResponseReceivedListener(response => {
|
|
|
|
|
console.log(response);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
notificationListener.current &&
|
|
|
|
|
Notifications.removeNotificationSubscription(notificationListener.current);
|
|
|
|
|
responseListener.current &&
|
|
|
|
|
Notifications.removeNotificationSubscription(responseListener.current);
|
|
|
|
|
};
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
return ({
|
|
|
|
|
schedulePushNotification: async (title, body, dataObj) => {
|
|
|
|
|
await schedulePushNotification(title, body, dataObj);
|
|
|
|
|
},
|
|
|
|
|
expoPushToken,
|
|
|
|
|
expoNotificationChannels: channels,
|
|
|
|
|
expoNotification: notification,
|
|
|
|
|
});
|
|
|
|
|
}
|