rewrite finished i believe

This commit is contained in:
2024-10-16 16:50:26 -05:00
parent c2858198ca
commit 3eeffb789f
15 changed files with 1184 additions and 86 deletions

View File

@ -1,5 +1,5 @@
import React, { useState, useEffect, useRef } from 'react';
import { Platform } from 'react-native';
import { Alert, Platform } from 'react-native';
import * as Device from 'expo-device';
import * as Notifications from 'expo-notifications';
import Constants from 'expo-constants';
@ -13,7 +13,11 @@ Notifications.setNotificationHandler({
}),
});
export const sendPushNotification = async(expoPushToken: string, notification: NotificationMessage) => {
export const sendPushNotification = async(expoPushToken: string | null, notification: NotificationMessage) => {
if (!expoPushToken) {
Alert.alert('Error', 'No push token found.');
return;
}
const message = {
to: expoPushToken,
sound: notification.sound ?? 'default',
@ -21,15 +25,22 @@ export const sendPushNotification = async(expoPushToken: string, notification: N
body: notification.body,
data: notification.data ?? {},
};
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),
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),
});
const result = await response.json();
console.log('Push notification sent:', result);
} catch (error) {
console.error('Error sending push notification:', error);
Alert.alert('Error', 'Failed to send push notification.');
}
};
const handleRegistrationError = (errorMessage: string) => {