rewrite finished i believe
This commit is contained in:
@ -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) => {
|
||||
|
Reference in New Issue
Block a user