2024-10-18 13:24:12 -05:00
|
|
|
import React, { useState, useEffect, useRef } from 'react';
|
2024-10-16 16:50:26 -05:00
|
|
|
import { Alert, Platform } from 'react-native';
|
2024-10-15 15:53:05 -05:00
|
|
|
import * as Device from 'expo-device';
|
|
|
|
import * as Notifications from 'expo-notifications';
|
|
|
|
import Constants from 'expo-constants';
|
2024-10-18 13:24:12 -05:00
|
|
|
import { NotificationMessage } from '@/constants/Types';
|
2024-10-15 15:53:05 -05:00
|
|
|
|
|
|
|
Notifications.setNotificationHandler({
|
|
|
|
handleNotification: async () => ({
|
|
|
|
shouldShowAlert: true,
|
2024-10-18 13:24:12 -05:00
|
|
|
shouldPlaySound: false,
|
|
|
|
shouldSetBadge: false,
|
2024-10-15 15:53:05 -05:00
|
|
|
}),
|
|
|
|
});
|
|
|
|
|
2024-10-16 16:50:26 -05:00
|
|
|
export const sendPushNotification = async(expoPushToken: string | null, notification: NotificationMessage) => {
|
|
|
|
if (!expoPushToken) {
|
|
|
|
Alert.alert('Error', 'No push token found.');
|
|
|
|
return;
|
|
|
|
}
|
2024-10-15 15:53:05 -05:00
|
|
|
const message = {
|
|
|
|
to: expoPushToken,
|
|
|
|
sound: notification.sound ?? 'default',
|
|
|
|
title: notification.title,
|
|
|
|
body: notification.body,
|
|
|
|
data: notification.data ?? {},
|
|
|
|
};
|
2024-10-16 16:50:26 -05:00
|
|
|
try {
|
|
|
|
const response = await fetch('https://exp.host/--/api/v2/push/send', {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
Accept: 'application/json',
|
|
|
|
'Accept-encoding': 'gzip, deflate',
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
body: JSON.stringify(message),
|
2024-10-15 15:53:05 -05:00
|
|
|
});
|
2024-10-16 16:50:26 -05:00
|
|
|
const result = await response.json();
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Error sending push notification:', error);
|
|
|
|
Alert.alert('Error', 'Failed to send push notification.');
|
|
|
|
}
|
2024-10-15 15:53:05 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
const handleRegistrationError = (errorMessage: string) => {
|
|
|
|
alert(errorMessage);
|
|
|
|
throw new Error(errorMessage);
|
|
|
|
};
|
|
|
|
|
2024-10-18 13:24:12 -05:00
|
|
|
async function registerForPushNotificationsAsync() {
|
2024-10-15 15:53:05 -05:00
|
|
|
let token;
|
|
|
|
|
|
|
|
if (Platform.OS === 'android') {
|
|
|
|
await Notifications.setNotificationChannelAsync('default', {
|
|
|
|
name: 'default',
|
|
|
|
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') {
|
|
|
|
const { status } = await Notifications.requestPermissionsAsync();
|
|
|
|
finalStatus = status;
|
|
|
|
}
|
|
|
|
if (finalStatus !== 'granted') {
|
|
|
|
alert('Failed to get push token for push notification!');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const projectId = Constants.expoConfig?.extra?.eas?.projectId;
|
|
|
|
if (!projectId) {
|
2024-10-18 13:24:12 -05:00
|
|
|
alert('Project ID not found');
|
2024-10-15 15:53:05 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
token = (await Notifications.getExpoPushTokenAsync({ projectId })).data;
|
|
|
|
} else {
|
|
|
|
alert('Must use physical device for Push Notifications');
|
|
|
|
}
|
2024-10-18 13:24:12 -05:00
|
|
|
|
2024-10-15 15:53:05 -05:00
|
|
|
return token;
|
2024-10-18 13:24:12 -05:00
|
|
|
}
|
2024-10-15 15:53:05 -05:00
|
|
|
|
2024-10-18 13:24:12 -05:00
|
|
|
export function PushNotificationManager({ children }: { children: React.ReactNode }) {
|
|
|
|
const [expoPushToken, setExpoPushToken] = useState<string | undefined>('');
|
|
|
|
const [notification, setNotification] = useState<Notifications.Notification | undefined>(undefined);
|
2024-10-15 15:53:05 -05:00
|
|
|
const notificationListener = useRef<Notifications.Subscription>();
|
|
|
|
const responseListener = useRef<Notifications.Subscription>();
|
|
|
|
|
|
|
|
useEffect(() => {
|
2024-10-18 13:24:12 -05:00
|
|
|
registerForPushNotificationsAsync().then(token => setExpoPushToken(token));
|
2024-10-15 15:53:05 -05:00
|
|
|
|
2024-10-18 13:24:12 -05:00
|
|
|
notificationListener.current = Notifications.addNotificationReceivedListener(notification => {
|
|
|
|
setNotification(notification);
|
2024-10-15 15:53:05 -05:00
|
|
|
});
|
2024-10-18 13:24:12 -05:00
|
|
|
|
|
|
|
responseListener.current = Notifications.addNotificationResponseReceivedListener(response => {
|
|
|
|
console.log(response);
|
|
|
|
// Handle notification response here
|
2024-10-15 15:53:05 -05:00
|
|
|
});
|
2024-10-18 13:24:12 -05:00
|
|
|
|
2024-10-15 15:53:05 -05:00
|
|
|
return () => {
|
2024-10-18 13:24:12 -05:00
|
|
|
Notifications.removeNotificationSubscription(notificationListener.current!);
|
|
|
|
Notifications.removeNotificationSubscription(responseListener.current!);
|
2024-10-15 15:53:05 -05:00
|
|
|
};
|
|
|
|
}, []);
|
2024-10-18 12:39:54 -05:00
|
|
|
|
2024-10-18 13:24:12 -05:00
|
|
|
return <>{children}</>;
|
2024-10-18 12:39:54 -05:00
|
|
|
}
|