42 lines
1.1 KiB
TypeScript
42 lines
1.1 KiB
TypeScript
import * as Notifications from 'expo-notifications'
|
|
import { Platform } from 'react-native'
|
|
import { getApiBaseUrl } from './api-url'
|
|
|
|
Notifications.setNotificationHandler({
|
|
handleNotification: async () => ({
|
|
shouldShowAlert: true,
|
|
shouldPlaySound: true,
|
|
shouldSetBadge: true,
|
|
shouldShowBanner: true,
|
|
shouldShowList: true,
|
|
}),
|
|
})
|
|
|
|
export async function setupPushNotifications() {
|
|
if (Platform.OS === 'web') return
|
|
|
|
const { status: existingStatus } = await Notifications.getPermissionsAsync()
|
|
let finalStatus = existingStatus
|
|
|
|
if (existingStatus !== 'granted') {
|
|
const { status } = await Notifications.requestPermissionsAsync()
|
|
finalStatus = status
|
|
}
|
|
|
|
if (finalStatus !== 'granted') return
|
|
|
|
const token = await Notifications.getExpoPushTokenAsync({
|
|
projectId: process.env.EXPO_PUBLIC_PROJECT_ID,
|
|
})
|
|
|
|
// Store push token on the server.
|
|
const apiUrl = getApiBaseUrl()
|
|
await fetch(`${apiUrl}/api/push-token`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ token: token.data }),
|
|
}).catch(() => {
|
|
// Silently fail — push is optional
|
|
})
|
|
}
|